Java thread 3: thread suspension and Restoration

Source: Internet
Author: User
1. Overview: The thread suspension operation is essentially to bring the thread into the "non-executable" state, in which the CPU will not be allocated to the thread time slice, this state can be used to pause the running of a thread. After the thread is suspended, it can be restarted to resume running. When a thread enters the "non-executable" state, that is, the suspension state, there must be some reason that it cannot continue running. These reasons may be the following situations:
(1) Call the sleep () method to bring the thread into sleep state, and the thread will not run within the specified time.
(2) The thread is suspended by calling the join () method, if a thread calls t on another thread t. join (), the thread will be suspended until the thread t has been executed.
(3) The thread is suspended by calling the wait () method until the thread gets the Y () and notifyall () messages, and the thread enters the "executable" state.
2. Sleep () method: A method used to temporarily stop a thread for a period of execution time, which is determined by the number of milliseconds. The following is an example of using sleep.
Public void run (){
Int COUNT = 3;
While (true ){
System. Out. println (this); // print the object
Try {
Sleep (1000); // sleep for 1000 ms
} Catch (exception e ){
E. printstacktrace ();
}
Count --; // variable count auto-Subtraction
If (COUNT = 0) return; // If count is 0, the loop exists.
}
}
The code is described as follows. First, define a variable count to control the exit of the loop condition, print the object in the loop, so that the thread can sleep for 1000 ms in the try block, and finally jump out of the loop when the variable is reduced from 3 to 0. When calling the sleep () method, you need to put this code into the try block. This is because if the thread is sleeping, the person holding this thread reference may call the interrupt () method, using the interrupt () method will return the thread from the "non-executable" execution status to the "executable" status, but it will cause Java. lang. interruptedexception exception.
3. Join () method: enables the current thread to stop and wait until the thread called by the join () method ends and resume execution. For example, if thread a is running and you want to insert thread B, and thread B is required to complete the execution and then continue thread A, you can use join () method to complete this requirement.
4. Wait () and notify () Methods: The wait () method can also cause the thread to suspend operations. The thread that calls the wait () method enters the "non-executable" state, there are two ways to use the wait () method, for example:
Thread. Wait (1000 );
Or:
Thread. Wait ();
Thread. Policy ();
The first method is used to specify the thread suspension time, which is basically the same as the sleep () method. The second method is to use the wait () and notify () methods in combination. This method allows the wait () method to wait infinitely until the thread receives the notify () or yyall () message.
Wait (), Policy (), and policyall () are different from other thread methods. These three methods are part of the Java. Lang. object class, so they are inherited when defining their own classes. Wait (), Policy (), and policyall () are declared as final classes, so they cannot be redefined.
5. Suspend () and resume () Methods
Sometimes it is better to force a thread to be suspended, instead of specifying a sleep time for the thread. In this case, other threads are responsible for awakening the thread to continue execution, except for the wait () and notify () methods, there is also a method in the thread to complete this function, which is the suspend () and resume () methods, thread. suspend (); thread. resume (); here, the thread is forcibly suspended after running to suspend () and paused until the main thread calls the thread. the resume () method is reawakened.
The suspend () and resume () methods have been deprecated in Java2, because using these two methods may lead to deadlocks, so you should use synchronous objects to call wait () and Policy () instead of suspend () and resume.

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.