[Multithreading] _ multi-thread notes

Source: Internet
Author: User

[Multithreading] _ multi-thread notes

Objectives of this chapter:
Measure the test taker's knowledge about the differences between processes and threads.
Understanding the two implementation methods and differences in Java threads
Understanding the thread operation status

3.1 processes and threads

The thread is actually further divided based on the process. From the word perspective, spelling check can be processed as a thread. Of course, there will be multiple threads at the same time.
If a process does not exist, the thread will definitely disappear. If the thread disappears, but the process may not disappear. In addition, some threads run concurrently on the basis of threads ).

3.2java multi-thread implementation
To implement multithreading in Java, you can use the following two methods:

3.2.1 Thread class
Inherit Thread class
The thread class is defined in the Java. lang package. A class is called a multi-threaded class as long as it inherits the Thread class. In the thread subclass, you must explicitly override the run () method, which is the main body of the thread.
Definition Syntax:

Class class name extends thread {
Attribute;
Method;
// Override the run () method in the Thread class. This method is the main body of the thread.
Public void run (){
Thread subject;
}
}

Instance 1:

Class mythread extends thread {// inherits the Thread class and serves as the thread implementation class private string name; // represents the thread name public mythread (string name) {This. name = Name;} public void run () {// override the run () method as the main thread operation body for (INT I = 0; I <10; I ++) {system. out. println (name + "run, I =" + I) ;}} public class threaddemo01 {public static void main (string ARGs []) {mythread MT1 = new mythread ("thread a"); mythread MT2 = new mythread ("thread B"); mt1.run (); mt2.run ();}}

The above code is to execute B after executing a, and does not achieve the so-called concurrent execution effect.
Because the above program is actually called in the old form, through the object method, but if you want to start a thread, you must use the START () method defined in the Thread class.
Once the START () method is called, The run () method is actually called.

Class mythread extends thread {// inherits the Thread class and serves as the thread implementation class private string name; // represents the thread name public mythread (string name) {This. name = Name;} public void run () {// override the run () method as the main thread operation body for (INT I = 0; I <10; I ++) {system. out. println (name + "run, I =" + I) ;}} public class threaddemo01 {public static void main (string ARGs []) {mythread MT1 = new mythread ("thread a"); mythread MT2 = new mythread ("thread B"); mt1.start (); mt2.start ();}}

3.2.2runnable Interface
Implement the runnable interface
In Java, you can also implement multiple threads by implementing the runnable interface. The runnable interface only defines one abstract method:
Public void run ();

Multithreading through the runnable interface:

Class class name implements runnable {
Attribute;
Method;
Public void run (){
Thread subject;
}
}

Example 2:

Class mythread implements runnable {private string name; Public mythread (string name) {This. name = Name;} public void run () {for (INT I = 0; I <10; I ++) {system. out. println (name + "run, I =" + I) ;}} public class runnabledemo01 {public static void main (string ARGs []) {mythread MT1 = new mythread ("thread a"); mythread MT2 = new mythread ("thread B"); thread T1 = new thread (MT1 ); thread t2 = new thread (MT2); t1.start (); t2.start ();}}

3.3. Thread class and runnable interface

3.3.1 connection between the Thread class and the runnable interface

From the definition format, we can find that the Thread class is also a subclass of the runnable interface.

Connection between the Thread class and the runnable interface

Thread class definition:
Public class thread extends object implements runnable

This method is very similar to the proxy mode.

3.3.2 differences between the Thread class and the runnable interface

Runnable can be used to share resources.

Class mythread extends thread {private int ticket = 5; Public void run () {for (INT I = 0; I <100; I ++) {If (this. ticket> 0) {system. out. println ("selling tickets: ticket =" + ticket) ;}}} public class threaddemo04 {public static void main (string ARGs []) {mythread MT1 = new mythread (); mythread MT2 = new mythread (); mythread mt3 = new mythread (); mt1.start (); mt2.start (); mt3.start ();}}

Class mythread implements runnable {private int ticket = 5; Public void run () {for (INT I = 0; I <100; I ++) {If (this. ticket> 0) {system. out. println ("selling tickets: ticket =" + ticket) ;}}} public class runnabledemo02 {public static void main (string ARGs []) {mythread MT1 = new mythread (); // use only the same runnable subclass thread T1 = new thread (MT1); thread t2 = new thread (MT1); thraed T3 = new thread (MT1); t1.start (); t2.start (); t3.start ();}}

3.3.3 use conclusions of the thread class and runnable Interfaces
Implementing the runnabel interface has the following obvious advantages over inheriting the Thread class:
It is suitable for multiple identical program codes to process the same resource.
This avoids the impact of single inheritance limitations.
The program robustness is enhanced, and the code can be shared by multiple threads. The code and data are independent.

3.4. thread status
Multithreading also has a fixed operation status in the operation:
Creation status: Prepare a multi-threaded object: thread t = new thread ();
Ready status: the start () method is called, waiting for CPU scheduling
Running status: run the run () method.
Blocking status: Pause execution. resources may be handed over to other threads for use.
Termination status (death status): The thread is finished and is no longer in use.

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.