The difference between sleep, wait, yield, join the

 

1.sleep () method

Currently executing thread to suspend execution within a specified time, but hold the lock! ! ! !

sleep () makes the current thread into the blocked state, it will not be executed within a specified time.

After the sleep time is up, although not grab the lock, but also other threads and grab CPU time slice, that is to say, the following code, the current time is printed once every second, twice the print interval is likely more than one second time that is printed is not continuous.

1  Import the java.text.SimpleDateFormat;
 2  Import java.util.Date;
 . 3  Import java.util.Scanner;
 . 4  // Thread 1: sleep 1000 milliseconds per print the current time
 5  @ Thread 2: Press Enter can interrupt the sleep state of the thread 1 
. 6  public  class the Test {
 . 7      public  static  void main (String [] args) {
 . 8          the thread T = new new the thread () {
 . 9              @Override
 10              public  void RUN () {
 . 11                  for ( int I = 0; I <= 10000000; I ++ ) {
12                     SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
13                     String s = sdf.format(new Date());
14                     System.out.println(s);
15                     try {
16                         Thread.sleep(1000);
17                     } catch (InterruptedException e) {
18                         System.out.println("结束");
19                         break;
20                     }
21                 }
22             }
23         };
24         t.start();
25         Thread t2 = new Thread() {
26             @Override
27             public void run() {
28                 System.out.println("按回车唤醒t");
29                 new Scanner(System.in).nextLine();
30                 t.interrupt();
31             }
32         };
33         t2.setDaemon(true);
34         t2.start();
35     }
36 }
View Code

2.wait () method
prior to notify or notifyAll method other thread calls the object, leading to the current thread to wait. I will put a lock! ! ! ! So that other threads have the opportunity to seize the lock.
The current thread must have a current object lock. If the current thread is not the owner of this lock, IllegalMonitorStateException will throw an exception.
Wake the current object lock waiting threads using notify or notifyAll method, you must have the same object lock, otherwise it will throw an exception IllegalMonitorStateException.
wait () and notify () function must be called in synchronized or synchronized block in. If it is not, although they would compile, but at runtime IllegalMonitorStateException exception occurs.

wait (time), the time comes, you do not need to wake up, you can grab your own re-lock, continue.

. 1  sychronized (A) {
 2      a.wait ();
 . 3      a.notify (); // random wake-up 
. 4      a.notifyAll (); // all wake- 
5 }
View Code

3.yield method
to pause the currently executing thread object. Hold the lock! ! ! !
yield () Causes the current thread just returned to an executable state, execution yield () thread is possible after entering into an executable state, on the spot to grab CPU time slice, has been executed, even, unlimited re-election! ! !
yield () can only make the thread priority or with higher priority have a chance to execute. I have given you the opportunity to make, and can not grabbed me, then look at priorities and resigned.
4.join method
Waits for this thread.
Call join method of waiting for the end of the thread, and then continue. Such as: t.join (); // t wait for the main thread running end, without sentence, main is completed will be executed, leading to unpredictable results.

Guess you like

Origin www.cnblogs.com/jmyyyy/p/10959904.html