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

买票问题-线程的同步

时间:2015-07-25 14:56:05      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

 1 //买票问题
 2 package ThreadL;
 3 
 4 public class Thread9 {
 5     public static void main(String[] args){
 6         Thread th1 = new Thread(new Thread9L(),"AAA");
 7         Thread th2 = new Thread(new Thread9L(),"BBBBBB");
 8         th1.start();
 9         th2.start();
10     }
11 }
12 class Thread9L implements Runnable{
13     static int ticket = 100;
14     static int count = 0;
15     public void run(){
16         while(true){
17             synchronized("L")    //动作原语
18             {
19                 if(ticket>0){
20 /*                    try {
21                         Thread.sleep(1000);
22                     } catch (InterruptedException e) {
23                         // TODO Auto-generated catch block
24                         e.printStackTrace();
25                     }
26 */                    System.out.println(Thread.currentThread().getName() + "\t出售第\t" + ticket-- + "\t张车票。");
27                     count++;
28                 }else{
29                     System.out.println(Thread.currentThread().getName() + "\t已售完。\t" + count);
30                     break;
31                 }
32             }
33         }
34     }
35 }

 

买票问题-线程的同步

标签:

原文地址:http://www.cnblogs.com/zzuLiang/p/4675810.html

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