多线程同步标记

 1 package duoxiancheng;
 2 
 3 public class MaiPiao implements Runnable{
 4 
 5     //当前票数
 6     private int count=10;
 7     @Override
 8     public void run() {
 9         // TODO Auto-generated method stub
10         
11         
12         // 卖票
13         while(true)
14         {
15             //线程同步块
16             synchronized ("同步标记:0;1") {
17                 try {
18                     Thread.sleep(100);
19                 } catch (InterruptedException e) {
20                     // TODO Auto-generated catch block
21                     e.printStackTrace();
22                 }
23                 if(count>0){
24                 count--;//卖出
25                 System.out.println(Thread.currentThread().getName()+"剩余票数="+count);
26                 }
27                 else
28                 {
29                     break;
30                 }
31             }
32             
33             
34             
35         }
36     }
37     
38     public static void main(String[] args)
39     {
40         MaiPiao m=new MaiPiao();
41         
42         Thread t1=new Thread(m,"售票点1");
43         Thread t2=new Thread(m,"售票点2");
44         Thread t3=new Thread(m,"售票点3");
45         t1.start();
46         t2.start();
47         t3.start();
48     }
49     
50     
51 
52 }
View Code

posted on 2016-03-16 13:56  beens  阅读(162)  评论(0编辑  收藏  举报

导航