日期:2014-05-20 浏览次数:20651 次
class Demo implements Runnable { private int ticket = 10 ; public synchronized void fun() { if (this.ticket >0) { try { Thread.sleep(100); } catch (Exception e) { } System.out.println(Thread.currentThread().getName()+"-->卖票:"+this.ticket--); } } public void run() { while(ticket>0) { this.fun(); } } } public class ThreadDemo07 { public static void main(String[] args) { Demo d = new Demo(); Thread t1 = new Thread(d,"A"); Thread t2 = new Thread(d,"B"); Thread t3 = new Thread(d,"C"); t1.start(); t2.start(); t3.start(); } }