Java multi-thread learning notes and multi-thread learning notes
Java multithreading 1. Introduction
Generally, the problem of concurrent ending can be divided into two types: "Speed" and "design manageability.
- Speed: multi-threaded
Frequent inter-thread SwitchingTo effectively avoid"Blocking"Problem;
- Design manageability: Improves code design. concurrent programming is more in line with people's actual logic and can effectively solve the problem.
SimulationRelated Questions.
Ii. java thread 1. Creation and startup
There are usually two ways to create a thread in java:
-
Extended java. lang. Thread class
Public class taskT extends Thread {public void run () {System. out. println ("Running Runnable! ") ;}Public static void main (String [] args) {Thread t = new taskT (); t. start ();}}
-
Implement the java. lang. Runnable interface
Public class taskR implements Runnable {@ Override public void run () {System. out. println ("Running Runnable! ") ;}} Public static void main (String [] args) {taskR r = new taskR (); Thread thread = new Thread (r); thread. start ();}
Once a new thread is created and executed, that is, when the start method is called, another process is added to the virtual machine, which is parallel to the original process.
Public class TaskR implements Runnable {@ Override public void run () {System. out. println ("executing thread B! "); System. out. println (" thread B has been executed! ") ;}} Public class test {public static void main (String [] args) {System. out. println (" the main function 111 is being executed! "); TaskR B = new TaskR (); Thread threadB = new Thread (B); System. out. println (" prepare to add Thread B! "); ThreadB. start (); System. out. println (" continue to execute main function 222! "); // For (int I = 0; I <999999; I ++); // The latency code System. out. println (" the main function 333 is being executed! ");}}
The output of the above program execution result is as follows:
Executing main function 111! Prepare to add thread B! Continue to execute main function 222! Executing main function 333! Executing thread B! Thread B has been executed!
Remove comments from the comments of the main function to increase code latency. The output is as follows:
Executing main function 111! Prepare to add thread B! Continue to execute main function 222! Executing thread B! Thread B has been executed! Executing main function 333!
From the comparison between the above two groups, it is not difficult to find that when thread threadB starts to execute (execute threadbB. start (), the main function thread and thread threadB are concurrent.
In addition, multithreading seems to give people the feeling that multiple tasks are performed at the same time. In fact, this is not the case. virtual machines give people the illusion of simultaneous execution through frequent switching processes.
2. thread state conversion
The thread state is the basis of thread control. The thread status can be divided into five categories: raw, waiting, blocking, sleep, and dead.
- Student: The thread has been added and is not executed;
- Start (): After the thread calls the start () method, it enters the waiting/running status;
- Yield (): In addition, the Thread being executed can also be passed through the Thread. the yield () method suspends the execution of the current thread and enters the runable state, concession other threads, but may also fail, this thread may be selected again after it enters the running state. (Note: The concession thread is followed by the execution thread, or re-execution, and the program test is followed by execution of unexecuted Code );
- Blocking: the thread stops running or cannot jump out of the run () method;
- Sleep (): Call the Thread. sleep () method to suspend the Thread for a period of time, that is, sleep, which can be used to help other processes obtain running opportunities;
- Dead: The thread is finished and the run () method is executed.
3. Thread Synchronization and lock
When multiple threads access mutually exclusive (interchangeable) data at the same time, they should be synchronized to protect data so that the two threads do not change it at the same time. It is usually implemented using the synchronized field, and can only be synchronized to the method or the synchronization code is fast.
If two threads want to execute the synchronized Method in the same class and the two threads use the same instance to call the method, only one thread can execute the method at a time, and the other must wait, wait until the lock is released.
4. Thread Switching
- Notify ()
- Policyall ()
- Wait ()
5. Thread Scheduling
- Sleep: Thread. sleep ()
- Priority
- Concession: Thread. yield ()
-
Merge: join () is used to add a thread B to thread a. Before thread B is executed, thread a cannot be executed.
Public class TaskR implements Runnable {@ Override public void run () {System. out. println ("executing thread B! "); Try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println (" Thread B has been executed! ") ;}} Public class test {public static void main (String [] args) throws InterruptedException {System. out. println (" the main function is being executed! "); TaskR B = new TaskR (); Thread threadB = new Thread (B); System. out. println (" prepare to add Thread B! "); ThreadB. start (); System. out. println ("executed main") ;}} public class test2 {public static void main (String [] args) throws InterruptedException {System. out. println ("the main function is being executed! "); TaskR B = new TaskR (); Thread threadB = new Thread (B); System. out. println (" prepare to add Thread B! "); ThreadB. start (); System. out. println ("threadB. join () "); threadB. join (); System. out. println ("executed main ");}}
The main execution results in test1 and test2 are as follows:
The main function is being executed! Prepare to add thread B! ThreadB. After join () is executed, main is executing thread B! Thread B has been executed!
Add the join () method to test2:
The main function is being executed! Prepare to add thread B! ThreadB. join () is executing thread B! Thread B has been executed! After running main
In threadB, sleep is called to give the main function a chance to execute this thread. As a result, we can see that after threadB calls the join method, even if the main function is sleep, it cannot be completed. This shows the function of the join method, that is, the main function can be continued only after threadB is executed.
- Daemon thread
Thread Pool
Java. util. concurrent. Executors. Example:
ExecutorService pool = Executors.newFixedThreadPool(2);Thread t1 = new MyThread(); Thread t2 = new MyThread();Thread t3 = new MyThread();Thread t4 = new MyThread();pool.execute(t1);pool.execute(t2);pool.execute(t3);pool.execute(t4);pool.shutdown();