JAVA notes: multi-thread understanding and application (1)
Processes and threads
A process is a dynamic execution process of a program. It goes through a complete process of code loading, execution, and execution. This process is also the lifecycle of the entire process.
Multithreading is an effective means of implementing the concurrency mechanism. Like threads, processes are the basic unit for implementing the concurrency mechanism.
In traditional single-core CPUs, multiple programs can be executed in the same time period, but only one program can run at a certain time point. All programs must seize CPU resources.
The program runs concurrently under multi-core CPU.
Implementation of multithreading in Java
To implement multithreading in Java, you can use the following two methods:
1. inherit the Thread class
2. Implement the Runnable interface
1. inherit the Thread class
The inherited Thread class is defined in the java. lang package. A Class must overwrite the run method in the Thread class as long as it inherits the Thread class. This method is the main body of the Thread.
Instance:
Class MyThread extends Thread {private String name; public MyThread (String name) {this. name = name;} public void run () {// override the run method as the main body of the thread operation (int I = 0; I <3; I ++) {System. out. println (name + test I =: + I) ;}} public class ThreadTest01 {public static void main (String [] args) {// TODO Auto-generated method stubMyThread mt1 = new MyThread (thread A); MyThread mt2 = new MyThread (thread B); mt1.run (); mt2.run ();}}
However, from the running results, the program does not achieve the effect of concurrent execution, because the method is still called in the format of object. method. To make them run concurrently, call the start method in the Thread class.
The call is slightly changed.
After the changes, we can see that the two programs are executed concurrently.
2. Implement the Runnable interface
To start a Thread, you must rely on the Thread class. If you have inherited the Thread class before, you can directly use the start method, but this method is not available in the Runnable interface.
The Thread class is constructed as follows: public Thread (Runnable target)
Use the preceding constructor to start multithreading.
Class MyThread implements Runnable {private String name; public MyThread (String name) {this. name = name;} public void run () {// override the run method as the main body of the thread operation (int I = 0; I <10; I ++) {System. out. println (name + test I =: + I) ;}} public class ThreadTest01 {public static void main (String [] args) {// TODO Auto-generated method stubMyThread 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 ();}}
Relationship between Thread class and Runnable interface
The Thread class is also a subclass of the Runnable interface.
RunnableInterfaces should be implemented by classes that intend to execute their instances through a thread. Class must be definedrun.
This interface is designed to provide a public protocol for objects that want to execute code during activity. For example,ThreadClassRunnable. Activation means that a thread has been started and has not been stopped.
Differences between the Thread class and the Runnable interface
The use of the Thread class cannot achieve the purpose of resource sharing, but the use of the Runnable class can achieve the purpose of resource sharing.
Ticket Selling instance:
Thread:
Class MyThread extends Thread {private int ticket = 5; private String name; public MyThread (String name) {this. name = name;} public void run () {// override the run method as the main body of the thread operation (int I = 0; I <10; I ++) {if (ticket> 0) {System. out. println (name + test I =: + ticket); ticket -- ;}}} public class ThreadTest01 {public static void main (String [] args) {// TODO Auto-generated method stubMyThread mt1 = new MyThread (thread A); MyThread mt2 = new MyThread (thread B); mt1.start (); mt2.start ();}}
Runnable:
Class MyThread implements Runnable {private int ticket = 5; private String name; public MyThread (String name) {this. name = name;} public void run () {// override the run method as the main body of the thread operation (int I = 0; I <10; I ++) {if (ticket> 0) {System. out. println (name + test I =: + ticket); ticket -- ;}}} public class ThreadTest01 {public static void main (String [] args) {// TODO Auto-generated method stubMyThread mt1 = new MyThread (Thread A); Thread t1 = new Thread (mt1); t1.run (); t1.run (); t1.run ();}}
The Runnable interface can be used to share resources.
Conclusion: The Runnable interface has more advantages than the inherited Thread class: 1. It is suitable for processing the same resource with Code of multiple identical programs. 2. Avoid the impact of single inheritance limitations. 3. The code can be shared by multiple processes, and the code and data are independent.
Thread status
Creation status: Prepare a multi-threaded object.
Ready: the Start method is called to wait for scheduling (the start method is to prepare for the Start and also to wait for CPU scheduling, which is actually to execute the run method)
Run: run the run method;
Blocking: Pause the thread and hand it over to other threads;
Termination: the thread execution is completed and is no longer in use.