Posted on 2009-07-18 06:53
WANGPENG 阅读(172)
评论(0) 编辑 收藏 所属分类:
Java
1 package com.ztejc.wp ;
2
3 public class SellTicket implements Runnable {
4 private static int ticketNum = 100 ;
5
6 public void run() {
7 while (ticketNum > 1) {
8 synchronized (this) {
9 ticketNum-- ;
10 }
11 System.out.println(Thread.currentThread().getName() + " : " + ticketNum) ;
12 }
13 }
14
15 public static void main(String[] args) {
16 SellTicket st = new SellTicket() ;
17
18 Thread thread1 = new Thread(st , "线程1") ;
19 Thread thread2 = new Thread(st , "线程2") ;
20 Thread thread3 = new Thread(st , "线程3") ;
21
22 thread1.start() ;
23 thread2.start() ;
24 thread3.start() ;
25 }
26 }
27