Java thread collaborative join (), java thread collaborative join
In my personal understanding, please do not spray for guidance.
Why is thread blocking.
1. When the current task (thread) needs to use the execution result of another task (thread), we need to block the current task (thread) and wait for the execution of another task (thread) to complete, get the processing result and continue execution.
2. When multiple threads access the critical zone (shared resource) at the same time, the resource is occupied by threads. Unable to obtain the relevant synchronization lock, so I had to enter the blocking status and wait until the synchronization lock is obtained to resume operation.
What is thread blocking?
The so-called blocking means that a thread can run, but a certain condition prevents it from running. When the thread is in a blocking state, the scheduler will ignore the thread and will not allocate any CPU time to the thread, it is not possible to execute operations until the thread enters the ready state again. Ready and running means that it is running. The so-called ready means it can run or not. The thread can run only after obtaining the time slice allocated by the scheduler. Thread blocking method:
Outline:
I. join ()
Ii. sleep ()
Iii. yield ()
4. wait ();
Join ()
The join () method mainly allows the thread that calls this method to complete the tasks in the run method, and then executes the generation after the join () method.
The main thread generates and starts the subthread, And the subthread requires a lot of time-consuming operations (here we can refer to the role of the subthread). After the main thread finishes processing other transactions, the processing result of the subthread needs to be used. In this case, the join (); method is used.
// This method will wait infinitely, And it will block the current thread until the execution of the target thread (the thread that calls this method) is completed.
Public final void join () throws InterruptedException {join (0 );}
// This method requires the maximum wait time. If the execution time of the target thread exceeds the specified time, the current thread will not directly execute public final synchronized void join (long millis) throws InterruptedException {} while waiting for the execution of the target thread to end {}
Public class ThreadJoinTest {private static String str = null; public static void main (String [] args) {Thread thread = new Thread () {@ Override public void run () {try {System. out. println ("enter" + Thread. currentThread (). getName () + "Thread"); Thread. sleep (1000); str = "hello Word"; System. out. println (Thread. currentThread (). getName () + "thread service processing completed");} catch (InterruptedException e) {e. printStackTrace () ;}}; thread. start (); try {// thread. join (); // set the main thread to wait for the sub-thread to process the business System first. out. println (Thread. currentThread (). getName () + "Start of thread processing"); System. out. println ("Get str value:" + str);} catch (Exception e) {e. printStackTrace ();}}}
Execution result:
The main thread starts processing the business.
Enter Thread-0 Thread
Get str value: null
Thread-0: the Thread service has been processed
Running the Code seems to never see the str value "hello Word", but every time it is null, the reason is very simple, this is because str is sleep for 1 second before being assigned a value in the run method of thread. out. the println method has been executed, so it is difficult to see that the str value is "hello Word", in order to see that the str value is "hello Word ", one idea is to execute System after the thread is finished. out. println is enough. At this time, the function of the join method is displayed. we delete the annotation code above and run it no matter how many times, the output result is "hello Word". In this example, we can see the function of the join method. It can adjust the running sequence between threads and implement synchronization.
If the code for modifying the annotation is thread. join (100), the execution result is: Get str value: null, because the main thread can no longer wait.
Join (), which allows the calling thread to wait on the current thread object. When the thread execution is complete, the waiting thread will call yyall () before the launch () method to notify all the waiting threads to continue execution.
public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }