Java Study Notes: java multithreading Summary

Source: Internet
Author: User

Thread introduction:

A thread, sometimes called a Lightweight Process (LWP), is the smallest unit of the program execution flow. A standard thread consists of the thread ID, current Instruction Pointer (PC), register set, and stack. In addition, a thread is an entity in a process and is the basic unit for independent scheduling and distribution by the system. A thread itself does not own system resources, but only has a few resources that are essential for running, however, it can share all resources of a process with other threads of the same process. One thread can create and withdraw another thread, and multiple threads in the same process can execute concurrently. Due to mutual constraints between threads, the threads are intermittently running. The thread also has three basic states: Ready, blocked, and running. Each program has at least one thread. If the program has only one thread, it is the program itself.

 


Multithreading in JAVA:


There are two methods to implement multithreading in java: one is to continue the Thread class and the other is to implement the Runable interface.

1. inherit the Thread directly


[Java]
Package com. yunos. account. controller. api;
 
 
Public class Test extends Thread {


Public void run (){
System. out. println ("Thread ID:" + Thread. currentThread ());
}

Public static void main (String [] args ){
Test t1 = new Test ();
Test t2 = new Test ();
T1.start ();
T2.start ();
}
}

Package com. yunos. account. controller. api;


Public class Test extends Thread {


Public void run (){
System. out. println ("Thread ID:" + Thread. currentThread ());
}
 
Public static void main (String [] args ){
Test t1 = new Test ();
Test t2 = new Test ();
T1.start ();
T2.start ();
}
}
Result:


[Html]
Thread ID: Thread [Thread-0, 5, main]
Thread ID: Thread [Thread-1, 5, main]

Thread ID: Thread [Thread-0, 5, main]
Thread ID: Thread [Thread-1, 5, main]

2. Implement the Runnable interface


[Java]
Package com. yunos. account. controller. api;
 
 
Public class Test implements Runnable {


Public void run (){
System. out. println ("Thread ID:" + Thread. currentThread ());
}

Public static void main (String [] args ){
Test test = new Test ();
Thread t1 = new Thread (test );
Thread t2 = new Thread (test );
T1.start ();
T2.start ();
}
}

Package com. yunos. account. controller. api;


Public class Test implements Runnable {


Public void run (){
System. out. println ("Thread ID:" + Thread. currentThread ());
}
 
Public static void main (String [] args ){
Test test = new Test ();
Thread t1 = new Thread (test );
Thread t2 = new Thread (test );
T1.start ();
T2.start ();
}
}
Result:


[Html]
Thread ID: Thread [Thread-0, 5, main]
Thread ID: Thread [Thread-1, 5, main]

Thread ID: Thread [Thread-0, 5, main]
Thread ID: Thread [Thread-1, 5, main]

Generally, we use the "implement Runnable interface" method.

Runnable has the following advantages over direct inheritance of Thread:

Suitable for processing the same resource by multiple threads with the same program code
Avoid the limitation of single inheritance in java
The program robustness is increased, and the code can be shared by multiple threads. The code and data are independent.
Commonly used threads:


[Java]
Package com. yunos. account. controller. api;
 
 
Public class Test implements Runnable {
 
Public void run (){
System. out. println (Thread. currentThread (). getName (); // print the Thread name
System. out. println (Thread. currentThread () + "Sleep start"); // print the Thread ID
Try {
Thread. sleep (2000); // Thread sleep for 2 seconds
} Catch (Exception e ){}
System. out. println (Thread. currentThread () + "sleep ends ");
}

Public static void main (String [] args ){
Test test = new Test ();
Thread t1 = new Thread (test, "Thread 1"); // you can name a Thread.
Thread t2 = new Thread (test, "Thread 2 ");
T2.setPriority (9); // sets the thread priority. The larger the value, the more advanced the execution
T1.setPriority (2 );
System. out. println ("status before t1 thread startup" + t1.isAlive (); // thread status
T1.setDaemon (true); // After setting true, the thread runs in the background even if the java Process is disabled.
T1.start ();
System. out. println ("status after t1 thread startup" + t1.isAlive ());
T1.interrupt (); // interrupt the sleep thread
T2.start ();
}
}

Package com. yunos. account. controller. api;


Public class Test implements Runnable {

Public void run (){
System. out. println (Thread. currentThread (). getName (); // print the Thread name
System. out. println (Thread. currentThread () + "Sleep start"); // print the Thread ID
Try {
Thread. sleep (2000); // Thread sleep for 2 seconds
} Catch (Exception e ){}
System. out. println (Thread. currentThread () + "sleep ends ");
}
 
Public static void main (String [] args ){
Test test = new Test ();
Thread t1 = new Thread (test, "Thread 1"); // you can name a Thread.
Thread t2 = new Thread (test, "Thread 2 ");
T2.setPriority (9); // sets the thread priority. The larger the value, the more advanced the execution
T1.setPriority (2 );
System. out. println ("status before t1 thread startup" + t1.isAlive (); // thread status
T1.setDaemon (true); // After setting true, the thread runs in the background even if the java Process is disabled.
T1.start ();
System. out. println ("status after t1 thread startup" + t1.isAlive ());
T1.interrupt (); // interrupt the sleep thread
T2.start ();
}
}
Returned results:


[Html]
T1 thread status before startup is false
The status of thread t1 after startup is true.
Thread 2
Thread [Thread, main] starts to sleep
Thread 1
Thread [Thread 1, 2, main] starts to sleep
Thread [Thread 1, 2, main] Sleep ends
Thread [Thread 2, 9, main] Sleep ends

T1 thread status before startup is false
The status of thread t1 after startup is true.
Thread 2
Thread [Thread, main] starts to sleep
Thread 1
Thread [Thread 1, 2, main] starts to sleep
Thread [Thread 1, 2, main] Sleep ends
Thread [Thread 2, 9, main] Sleep ends

 

 


 

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.