cool hit counter The difference between sleep and wait in java_Intefrankly

The difference between sleep and wait in java


For the sleep() method, we first need to know that the method belongs in the Thread class. The wait() method, on the other hand, belongs in the Object class.

The sleep() method causes the program to suspend execution for a specified amount of time, leaving the cpu free for that other thread, but his monitor state remains and will automatically resume running when the specified time is up.

During the call to the sleep() method, the thread does not release the object lock.

And when the wait() method is called, the thread gives up the object lock and enters the waiting lock pool waiting for this object, and only after the notify() method is called for this object does this thread enter the object lock pool ready

Get the object lock to enter the running state.

What does that mean?

As an illustration.

 1 /**
 2  * 
 3  */
 4 package com.b510.test;
 5 
 6 /**
  7 * The difference between sleep() and wait() in java
 8  * @author Hongten
 9  * @date 2013-12-10
10  */
11 public class TestD {
12 
13     public static void main(String[] args) {
14         new Thread(new Thread1()).start();
15         try {
16             Thread.sleep(5000);
17         } catch (Exception e) {
18             e.printStackTrace();
19         }
20         new Thread(new Thread2()).start();
21     }
22     
23     private static class Thread1 implements Runnable{
24         @Override
25         public void run(){
26             synchronized (TestD.class) {
27             System.out.println("enter thread1...");    
28             System.out.println("thread1 is waiting...");
29             try {
 30 // Calling the wait() method causes the thread to relinquish the object lock and enter the waiting lock pool that waits for this object
31                 TestD.class.wait();
32             } catch (Exception e) {
33                 e.printStackTrace();
34             }
35             System.out.println("thread1 is going on ....");
36             System.out.println("thread1 is over!!!");
37             }
38         }
39     }
40     
41     private static class Thread2 implements Runnable{
42         @Override
43         public void run(){
44             synchronized (TestD.class) {
45                 System.out.println("enter thread2....");
46                 System.out.println("thread2 is sleep....");
 47 // Only after the notify() method is called for this object does this thread enter the object lock pool ready to acquire the object lock into the running state.
48                 TestD.class.notify();
49                 //==================
 50 // Difference
 51 // If we take the code: testD.class.notify(); Comment out, i.e. TestD.class calls the wait() method but not notify()
 52 // method, then the thread will always be in a hung state.
53                 try {
 54 //sleep() method causes the program to suspend execution for a specified amount of time, giving up the cpu for that other thread.
 55 // But his monitoring state remains the same and will automatically resume running again when the specified time is up.
 56 // During the call to the sleep() method, the thread does not release the object lock.
57                     Thread.sleep(5000);
58                 } catch (Exception e) {
59                     e.printStackTrace();
60                 }
61                 System.out.println("thread2 is going on....");
62                 System.out.println("thread2 is over!!!");
63             }
64         }
65     }
66 }

Running effect.

enter thread1...
thread1 is waiting...
enter thread2....
thread2 is sleep....
thread2 is going on....
thread2 is over!!!
thread1 is going on ....
thread1 is over!!!

If commenting out the code.

1 TestD.class.notify();

Running effect.

enter thread1...
thread1 is waiting...
enter thread2....
thread2 is sleep....
thread2 is going on....
thread2 is over!!!

and the program stays hung.

E | hongtenzone@foxmail.com B |http://www.cnblogs.com/hongten


Recommended>>
1、Childrens group activity Lets play with robots
2、Quansu Health House Project Leverages Big Data Big Sharing and Big Health to Create 100000 Stores in 100 Cities
3、The latest February 2018 issue of CARSTYLING
4、Easy Rise WIN10 Turn Off Automatic Updates
5、PHP Tutorials Baidu Cloud

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号