前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java学习之协调同步的线程

java学习之协调同步的线程

作者头像
Gxjun
发布2018-03-26 11:19:55
9110
发布2018-03-26 11:19:55
举报
文章被收录于专栏:mlml

            当一个线程使用的同步方法中用到某个变量,而此变量有需要其他线程修改后才能符合本线程的需要,

     那么可以在同步方法中使用wait(),wait方法可以中断线程的执行,使本线程等待,暂时让出CPU的使用权,并允许其他线程使用这个同步方法。

     其他线程如果在使用这个同步方法时,不许需要等待,那么它使用这个同步方法。其他线程如果再使用这个同步方法是不需要等待,那么它使用完

     这个同步方法的同时,应当用notifyAll()方法通知所有由于使用这个同步方法而处于等待的线程结束等待.曾中断的线程就会从刚才的中断处继续执行

     这个同步方法.....

代码语言:javascript
复制
1 /*
2       wait();    
3       notify();
4       notifyAll();
5 */

   在下面的列子中:

代码语言:javascript
复制
/*
    张飞和李逵买电影票。售票员只有两张5元的钱,电影票5元一张。张飞
   拿20元一张的人民币排在李逵的前面买票,李逵拿一张5元的人民币买票,因此张飞必须等待。
*/
代码语言:javascript
复制
 1 //package Scan_boobs;
 2 public class Example12_8 extends window_scan
 3 {
 4     public static void main(String args [] )
 5     {
 6       TicketHouse officer = new TicketHouse();
 7       Thread zhangfei ,likui;
 8       zhangfei = new Thread(officer);
 9       zhangfei.setName("张飞");
10       likui = new Thread(officer);
11       likui.setName("李逵");
12       zhangfei.start();
13       likui.start();
14     }
15 }
16 
17 class TicketHouse implements Runnable
18 {
19     int fiveAmount=2,tenAmount=0,twentyAmount=0;
20     @Override
21     public void run() {
22         // TODO Auto-generated method stub
23        String name=Thread.currentThread().getName();
24         if(name.equals("张飞")) saleTicket(20);
25         else  saleTicket(5);
26         
27     }
28   private synchronized void saleTicket(int money)
29   {
30     if(money==5) 
31     {
32       fiveAmount++;
33       this.out("给"+Thread.currentThread().getName()+"入场卷 ,"
34               +Thread.currentThread().getName()+"的钱正好");
35     }
36     else if(money==20)
37     {
38         while(fiveAmount<3)
39         {
40             try {
41                  this.out("\n"+Thread.currentThread().getName()+"靠边等....");
42                  wait();
43               // Thread.sleep(3000);
44                   this.out("\n"+Thread.currentThread().getName()+"继续买票");
45             } catch (InterruptedException e) {
46                 // TODO Auto-generated catch block
47                 e.printStackTrace();
48             }
49         }
50         fiveAmount-=3;
51         twentyAmount++;
52         this.out("给"+Thread.currentThread().getName()+"入场卷,"
53                 +Thread.currentThread().getName()+"给20,找赎15元");
54     }
55     notifyAll();
56   }
57  private void out(String name)
58  {
59   System.out.println(name);   
60  } 
61 }

需要特别注意:

代码语言:javascript
复制
/*
     在许多实际的问题中wait方法应当放在一个"while(等待的条件){}"的循环语句中,而不是“if(等待条件){}的分支语句中”
*/

如果咸的蛋疼了,将wait();  ----》改为 Thread.sleep(); 然后呵呵,你会就会这样   

代码:

代码语言:javascript
复制
 1 package Scan_boobs;
 2 public class Example12_8 extends window_scan
 3 {
 4     public static void main(String args [] )
 5     {
 6       TicketHouse officer = new TicketHouse();
 7       Thread zhangfei ,likui;
 8       zhangfei = new Thread(officer);
 9       zhangfei.setName("张飞");
10       likui = new Thread(officer);
11       likui.setName("李逵");
12       zhangfei.start();
13       likui.start();
14     }
15 }
16 
17 class TicketHouse implements Runnable
18 {
19     int fiveAmount=2,tenAmount=0,twentyAmount=0;
20     @Override
21     public void run() {
22         // TODO Auto-generated method stub
23        String name=Thread.currentThread().getName();
24         if(name.equals("张飞")) saleTicket(20);
25         else  saleTicket(5);
26         
27     }
28   private synchronized void saleTicket(int money)
29   {
30     if(money==5) 
31     {
32       fiveAmount++;
33       this.out("给"+Thread.currentThread().getName()+"入场卷 ,"
34               +Thread.currentThread().getName()+"的钱正好");
35     }
36     else if(money==20)
37     {
38         while(fiveAmount<3)
39         {
40             try {
41                  this.out("\n"+Thread.currentThread().getName()+"靠边等....");
42                  wait();
43               // Thread.sleep(3000);
44                   this.out("\n"+Thread.currentThread().getName()+"继续买票");
45             } catch (InterruptedException e) {
46                 // TODO Auto-generated catch block
47                 e.printStackTrace();
48             }
49         }
50         fiveAmount-=3;
51         twentyAmount++;
52         this.out("给"+Thread.currentThread().getName()+"入场卷,"
53                 +Thread.currentThread().getName()+"给20,找赎15元");
54     }
55     notifyAll();
56   }
57  private void out(String name)
58  {
59   System.out.println(name);   
60  } 
61 }

效果图:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档