Java thread Basics

Source: Internet
Author: User

Why multithreading?

 

. Model simplification. For example, some programs run by multiple relatively independent tasks:

The appearance of the graphic interface, the obstruction of Input and Output

. Better use of multi-core CPU

. Asynchronous behavior needs

 

Java multithreading features:

Main itself is a thread.

. Threads are concurrent and unordered.

The threads are executed sequentially.

. Share Data

 

Java multithreading risks:

. Security Risk: because the operation sequence of the thread is uncertain, some programs that can run in a single thread may encounter unexpected results under multiple threads.

. Performance risks: Server throughput, responsiveness, and resource consumption

 

Java multi-thread API:

Java can create threads in two forms: 1. Implement the Runnable interface and 2. inherit the Thread class.

 

Sample Code for inheriting thread Creation
  1. 0; I <10; I ++ ){
  2. "Main:" + I );
  3. 0; I <10; I ++ ){
  4. "Thread:" + I );

 

Implement runnable thread creation code
  1. 0; I <10; I ++ ){
  2. "Main:" + I );
  3. 0; I <10; I ++ ){
  4. "Thread:" + I );

 

 

Java thread pool technology: Executors obtains the exceuctorservice thread pool code
  1. 99 );
  2. "T1 ");
  3. "T2 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.