Thread Suspend and resume reprint

Source: Internet
Author: User

(1) Overview: The thread's suspend operation is essentially to make the thread into a "non-executable" state, in this state the CPU will not be assigned to the thread time slice, enter this state can be used to suspend a thread running. After a thread is suspended, it can be resumed by re-awakening the threads.

Run () and start () are two ways that everyone is familiar with. The code that wants to work in parallel is placed in run () and stat () is used to automatically call run (), which is defined by the inherent mechanism of java. When a thread enters a "non-executable" state, there must be some reason why it cannot continue running, for several reasons:
A, the thread goes into hibernation by calling the sleep () method, and the thread does not run for a specified period of time.
B, the thread is suspended by calling the join () method, and if a thread is T.join () on another thread T, the thread will be suspended until the thread T finishes executing.
C, by calling the Wait () method to suspend the thread until the thread gets the Notify () and Notifyall () messages, the thread will enter the "executable" state.
(2) Sleep () method: is a method that causes a thread to temporarily stop execution time, which is determined by the given number of milliseconds. The following shows an example of using the sleep () method, as follows.

[Java]View PlainCopy
  1. Class Threada extends Thread
  2. {
  3. public Void Run () {
  4. System.out.println ("Threada is running");
  5. }
  6. }
  7. Public class Testnew {
  8. public static void Main (string[] args)throws interruptedexception {
  9. //TODO auto-generated method stub
  10. Threada ta = new Threada ();
  11. Ta.start ();
  12. Ta.sleep (5000);
  13. System.out.println ("testnew is running");
  14. }
  15. }

The execution result is: First Threada is running,5 seconds, Testnew is running. Some people will ask: Ta.sleep (5000); This statement is the TA thread sleeps for 5 seconds, why does the main thread of the execution result is also sleep for 5 seconds? The two are two separate line friend pairs? The reason is: in which thread declares sleep, which thread sleeps, so the main thread sleeps for 5000 milliseconds = 5 seconds.

(3) Join () Method: Enables the currently executing thread to stop waiting until the thread called by the Join () method ends and resumes execution. For example, if a thread A is running, the user wants to insert a thread B, and requires thread B to complete before continuing with thread A, the join () method can be used to accomplish this requirement.

[Java]View PlainCopy
  1. Public class Testnew extends Thread
  2. {
  3. public static int a = 0;
  4. public Void Run () {
  5. For (int k = 0;k < 5;k + +) {
  6. A = a + 1;
  7. }
  8. }
  9. public static void Main (string[] args)throws interruptedexception {
  10. //TODO auto-generated method stub
  11. Testnew ta = new Testnew ();
  12. Ta.start ();
  13. Ta.join ();
  14. System.out.println (String.valueof (a));
  15. }
  16. }

Execution result: 5. The TA thread does not start printing a until it finishes executing. If there is no join (), the result is not necessarily 5, because the TA thread does not have a fixed order of execution with the main thread.
(4) The Wait () and Notify () method: The Wait () method also enables the thread to suspend operations, the thread that calls the wait () method enters a "non-executable" state, and there are two ways to use the Wait () method, for example:
Thread.wait (1000);
Or:
Thread.wait ();
Thread.notify ();
The first of these methods is the same as the sleep () method usage, given the thread suspend time. The second approach is to use wait () with the Notify () method, which allows the wait () method to wait indefinitely until the thread receives the Notify () or Notifyall () message.
Wait (), notify (), Notifyall () are different from other threading methods, and these 3 methods are part of the Java.lang.Object class, so they are inherited when defining their own classes. Wait (), notify (), Notifyall () are declared as final classes, so they cannot be redefined.
(5) Suspend () and resume () method
Sometimes it is better to suspend a thread rather than to specify a sleep time for the thread, in which case the other thread is responsible for waking it to continue execution, and in addition to the wait () and notify () methods, there is a pair of methods in the thread to complete this function, which is suspend () and resume () Method. Thread.Suspend (); Thread.Resume (), thread threads are forced to suspend after running to suspend () and are paused until the main thread calls the Thread.Resume () method and is awakened again.
The suspend () and resume () methods have been deprecated in Java2 because using these two methods may result in deadlocks, so you should use synchronization objects to invoke the mechanism of Wait () and notify () instead of suspend () and resume () for thread control.

Excerpt from: Self-paced Java programming manual

Reference Original: http://blog.csdn.net/zhandoushi1982/article/details/5506597

Thread Suspend and resume reprint

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.