码迷,mamicode.com
首页 > 编程语言 > 详细

java object wait notify notifyAll 的探究

时间:2020-11-08 16:50:52      阅读:19      评论:0      收藏:0      [点我收藏+]

标签:object   date()   catch   rac   obj   override   text   其他   form   

先列出我的测试代码:

 1 public static Object loc=new Object();
 2 
 3     public static void main(String[] args)  throws Exception{
 4 
 5         Thread t1=new Thread(new Runnable() {
 6             @Override
 7             public void run() {
 8                 try {
 9                     java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS");
10                     System.out.println("["+sdf.format(new Date())+"] t1___等待锁...");
11                     synchronized (loc) {
12                         System.out.println("["+sdf.format(new Date())+"] t1___获得锁 ...");
13                         Thread.sleep(5000);
14                         System.out.println("["+sdf.format(new Date())+"] t1___loc..开始执行wait...");
15                         loc.wait();
16                         System.out.println("["+sdf.format(new Date())+"] t1___loc..执行wait后续...");
17                         Thread.sleep(1000);
18                     }
19                     System.out.println("["+sdf.format(new Date())+"] t1___loc..离开锁...");
20                 }catch (Exception e){
21                     e.printStackTrace();
22                 }
23             }
24         });
25         Thread t3=new Thread(new Runnable() {
26             @Override
27             public void run() {
28                 try {
29                     java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS");
30                     System.out.println("["+sdf.format(new Date())+"] t3___等待锁...");
31                     synchronized (loc) {
32                         System.out.println("["+sdf.format(new Date())+"] t3___进入锁...");
33                         Thread.sleep(5000);
34                         System.out.println("["+sdf.format(new Date())+"] t3___loc..开始 wait...");
35                         loc.wait();
36                         System.out.println("["+sdf.format(new Date())+"] t3___loc..执行 wait后续...");
37                         Thread.sleep(1000);
38                     }
39                     System.out.println("["+sdf.format(new Date())+"] t3___离开锁...");
40                 }catch (Exception e){
41                     e.printStackTrace();
42                 }
43             }
44         });
45 
46         Thread t2=new Thread(new Runnable() {
47             @Override
48             public void run() {
49                 try {
50                     java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS");
51                     System.out.println("["+sdf.format(new Date())+"] t2___等待锁...");
52                     synchronized (loc) {
53                         System.out.println("["+sdf.format(new Date())+"] t2___获得锁...");
54                         Thread.sleep(5000);
55                         System.out.println("["+sdf.format(new Date())+"] t2___loc..开始执行 notify...");
56                         loc.notify();
57                         System.out.println("["+sdf.format(new Date())+"] t2___loc..执行 notify后续...");
58                         Thread.sleep(1000);
59                     }
60                     System.out.println("["+sdf.format(new Date())+"] t2___loc 离开锁....");
61                 }catch (Exception e){
62                     e.printStackTrace();
63                 }
64             }
65         });
66         t1.start();
67         t3.start();
68         Thread.sleep(500);
69         t2.start();
70 
71 
72         System.out.println("t1___before join ....");
73         t1.join();
74         System.out.println("t2___before join ....");
75         t2.join();
76         System.out.println("t3____before join ....");
77         t3.join();
78         System.out.println("main exit....");
79     }

执行结果:

  

[16:55:59 384] t1___等待锁...
[16:55:59 384] t1___获得锁 ...
[16:55:59 384] t3___等待锁...
t1___before join ....
[16:55:59 836] t2___等待锁...
[16:56:04 392] t1___loc..开始执行wait...

[16:56:04 392] t2___获得锁...
[16:56:09 392] t2___loc..开始执行 notify...
[16:56:09 392] t2___loc..执行 notify后续...
[16:56:10 392] t2___loc 离开锁....


[16:56:10 392] t3___进入锁...
[16:56:15 392] t3___loc..开始 wait...
[16:56:15 392] t1___loc..执行wait后续...
[16:56:16 392] t1___loc..离开锁...
t2___before join ....
t3____before join ....

 

总结:

  1.执行wait后‘’暂时‘ 释放当前对象锁给其他线程,当前线程处于等待状态

  2.syn块中的wait收到notify通知后 唤醒cpu 然后继续判断对象锁状态

  2.执行notify且当前的对象锁释放后  wait等待的线程激活继续执行

 

java object wait notify notifyAll 的探究

标签:object   date()   catch   rac   obj   override   text   其他   form   

原文地址:https://www.cnblogs.com/dint/p/13926904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!