Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 286|回复: 0

[Java线程学习]线程基础---wait(),notify的应用一例

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-29 23:58:13 | 显示全部楼层 |阅读模式
    本例子实现了两个线程,每个线程输出1到100的数字。 第一个线程输出1-10,停止,通知第二个线程 输出1-10 第二个线程停止 通知第一个线程 输出11-20 ...

         实现的要点是 在java中,每个对象都有个对象锁标志(Object lock flag)与之想关联,当一个线程A调用对象的一段synchronized代码时, 它首先要获取与这个对象关联的对象锁标志,然后执行相应的代码,执行结束后,把这个对象锁标志返回给对象;因此,在线程A执行 synchronized代码期间,如果另一个线程B也要执行同一对象的一段synchronized代码时(不一定与线程A执行的相同),它将要等到线程A执行完后,才能继续.... 如何利用wait() notify() notifyAll()?

        在synchronized代码被执行期间,线程可以调用对象的wait()方法,释放对象锁标志,进入等待状态,并且可以调用notify()或者 notifyAll()方法通知正在等待的其他线程。notify()通知等待队列中的第一个线程,notifyAll()通知的是等待队列中的所有线程。  
      
       
       
         
       

       
       
      
       


    1.   
    2. /**
    3. * Title:        Jasons"s Java Projdect
    4. * Copyright:    Copyright (c) ChinaJavaLab
    5. * Company:      http://www.ChinaJavaLab.com
    6. * @author       jason jason@ChinaJavaLab.com
    7. * @version 1.0
    8. */
    9. import java.lang.Runnable;
    10. import java.lang.Thread;
    11. public class DemoThread implements Runnable{
    12.   public DemoThread() {
    13.          TestThread testthread1 = new TestThread(this,"1");
    14.          TestThread testthread2 = new TestThread(this,"2");
    15.          testthread2.start();
    16.          testthread1.start();
    17.   }
    18.   public static void main(String[] args) {
    19.     DemoThread demoThread1 = new DemoThread();
    20.   }
    21.    public void run(){
    22.         TestThread t = (TestThread) Thread.currentThread();
    23.         try{
    24.           if (!t.getName().equalsIgnoreCase("1")) {
    25.               synchronized(this) {
    26.                   wait();
    27.               }
    28.           }
    29.           while(true){
    30.             System.out.println("@time in thread"+ t.getName()+ "="+ t.increaseTime());
    31.             if(t.getTime()%10 == 0) {
    32.               synchronized(this) {
    33.                 System.out.println("****************************************");
    34.                 notify();
    35.                 if ( t.getTime()==100 ) break;
    36.                 wait();
    37.             }
    38.           }
    39.         }
    40.         }catch(Exception e){e.printStackTrace();}
    41.     }
    42. }
    43. class TestThread extends Thread{
    44.     private int time = 0 ;
    45.     public TestThread(Runnable r,String name){
    46.       super(r,name);
    47.     }
    48.     public int getTime(){
    49.        return time;
    50.     }
    51.     public int increaseTime (){
    52.        return ++time;
    53.     }
    54. }
    55. 程序运行结果:
    56. C:java>java   DemoThread
    57. @time in thread1=1
    58. @time in thread1=2
    59. @time in thread1=3
    60. @time in thread1=4
    61. @time in thread1=5
    62. @time in thread1=6
    63. @time in thread1=7
    64. @time in thread1=8
    65. @time in thread1=9
    66. @time in thread1=10
    67. ****************************************
    68. @time in thread2=1
    69. @time in thread2=2
    70. @time in thread2=3
    71. @time in thread2=4
    72. @time in thread2=5
    73. @time in thread2=6
    74. @time in thread2=7
    75. @time in thread2=8
    76. @time in thread2=9
    77. @time in thread2=10
    78. ****************************************
    79. @time in thread1=11
    80. @time in thread1=12
    81. @time in thread1=13
    82. @time in thread1=14
    83. @time in thread1=15
    84. @time in thread1=16
    85. @time in thread1=17
    86. @time in thread1=18
    87. @time in thread1=19
    88. @time in thread1=20
    89. ****************************************
    90. @time in thread2=11
    91. @time in thread2=12
    92. @time in thread2=13
    93. @time in thread2=14
    94. @time in thread2=15
    95. @time in thread2=16
    96. @time in thread2=17
    97. @time in thread2=18
    98. @time in thread2=19
    99. @time in thread2=20
    100. ****************************************
    101. @time in thread1=21
    102. @time in thread1=22
    103. @time in thread1=23
    104. @time in thread1=24
    105. @time in thread1=25
    106. @time in thread1=26
    107. @time in thread1=27
    108. @time in thread1=28
    109. @time in thread1=29
    110. @time in thread1=30
    111. ****************************************
    112. @time in thread2=21
    113. @time in thread2=22
    114. @time in thread2=23
    115. @time in thread2=24
    116. @time in thread2=25
    117. @time in thread2=26
    118. @time in thread2=27
    119. @time in thread2=28
    120. @time in thread2=29
    121. @time in thread2=30
    122. ****************************************
    123. @time in thread1=31
    124. @time in thread1=32
    125. @time in thread1=33
    126. @time in thread1=34
    127. @time in thread1=35
    128. @time in thread1=36
    129. @time in thread1=37
    130. @time in thread1=38
    131. @time in thread1=39
    132. @time in thread1=40
    133. ****************************************
    134. @time in thread2=31
    135. @time in thread2=32
    136. @time in thread2=33
    137. @time in thread2=34
    138. @time in thread2=35
    139. @time in thread2=36
    140. @time in thread2=37
    141. @time in thread2=38
    142. @time in thread2=39
    143. @time in thread2=40
    144. ****************************************
    145. @time in thread1=41
    146. @time in thread1=42
    147. @time in thread1=43
    148. @time in thread1=44
    149. @time in thread1=45
    150. @time in thread1=46
    151. @time in thread1=47
    152. @time in thread1=48
    153. @time in thread1=49
    154. @time in thread1=50
    155. ****************************************
    156. @time in thread2=41
    157. @time in thread2=42
    158. @time in thread2=43
    159. @time in thread2=44
    160. @time in thread2=45
    161. @time in thread2=46
    162. @time in thread2=47
    163. @time in thread2=48
    164. @time in thread2=49
    165. @time in thread2=50
    166. ****************************************
    167. @time in thread1=51
    168. @time in thread1=52
    169. @time in thread1=53
    170. @time in thread1=54
    171. @time in thread1=55
    172. @time in thread1=56
    173. @time in thread1=57
    174. @time in thread1=58
    175. @time in thread1=59
    176. @time in thread1=60
    177. ****************************************
    178. @time in thread2=51
    179. @time in thread2=52
    180. @time in thread2=53
    181. @time in thread2=54
    182. @time in thread2=55
    183. @time in thread2=56
    184. @time in thread2=57
    185. @time in thread2=58
    186. @time in thread2=59
    187. @time in thread2=60
    188. ****************************************
    189. @time in thread1=61
    190. @time in thread1=62
    191. @time in thread1=63
    192. @time in thread1=64
    193. @time in thread1=65
    194. @time in thread1=66
    195. @time in thread1=67
    196. @time in thread1=68
    197. @time in thread1=69
    198. @time in thread1=70
    199. ****************************************
    200. @time in thread2=61
    201. @time in thread2=62
    202. @time in thread2=63
    203. @time in thread2=64
    204. @time in thread2=65
    205. @time in thread2=66
    206. @time in thread2=67
    207. @time in thread2=68
    208. @time in thread2=69
    209. @time in thread2=70
    210. ****************************************
    211. @time in thread1=71
    212. @time in thread1=72
    213. @time in thread1=73
    214. @time in thread1=74
    215. @time in thread1=75
    216. @time in thread1=76
    217. @time in thread1=77
    218. @time in thread1=78
    219. @time in thread1=79
    220. @time in thread1=80
    221. ****************************************
    222. @time in thread2=71
    223. @time in thread2=72
    224. @time in thread2=73
    225. @time in thread2=74
    226. @time in thread2=75
    227. @time in thread2=76
    228. @time in thread2=77
    229. @time in thread2=78
    230. @time in thread2=79
    231. @time in thread2=80
    232. ****************************************
    233. @time in thread1=81
    234. @time in thread1=82
    235. @time in thread1=83
    236. @time in thread1=84
    237. @time in thread1=85
    238. @time in thread1=86
    239. @time in thread1=87
    240. @time in thread1=88
    241. @time in thread1=89
    242. @time in thread1=90
    243. ****************************************
    244. @time in thread2=81
    245. @time in thread2=82
    246. @time in thread2=83
    247. @time in thread2=84
    248. @time in thread2=85
    249. @time in thread2=86
    250. @time in thread2=87
    251. @time in thread2=88
    252. @time in thread2=89
    253. @time in thread2=90
    254. ****************************************
    255. @time in thread1=91
    256. @time in thread1=92
    257. @time in thread1=93
    258. @time in thread1=94
    259. @time in thread1=95
    260. @time in thread1=96
    261. @time in thread1=97
    262. @time in thread1=98
    263. @time in thread1=99
    264. @time in thread1=100
    265. ****************************************
    266. @time in thread2=91
    267. @time in thread2=92
    268. @time in thread2=93
    269. @time in thread2=94
    270. @time in thread2=95
    271. @time in thread2=96
    272. @time in thread2=97
    273. @time in thread2=98
    274. @time in thread2=99
    275. @time in thread2=100
    276. ****************************************
    复制代码

    C:java>
      
       
       
         
         

          
         

          
         
       
      



      



                            function TempSave(ElementID)
                            {
                                    CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
                                    CommentsPersistDiv.save("CommentXMLStore");
                            }
                            function Restore(ElementID)
                            {
                                    CommentsPersistDiv.load("CommentXMLStore");
                                    document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
                   
                      











    源码下载:http://file.javaxxz.com/2014/10/29/235812750.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 23:29 , Processed in 0.361765 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表