标签:rri close span current nbsp java pen 多线程 runnable
/** * 这样买票会出现卖出超过100张票,把num静态化可以解决这个问题 */ private static void test1(){ System.out.println("开始卖票"+Thread.currentThread().getName()); Ticket ticket= new Ticket(); Thread t1=new Thread(ticket); Thread t2=new Thread(ticket); Thread t3=new Thread(ticket); Thread t4=new Thread(ticket); t1.start(); t2.start(); t3.start(); t4.start(); System.out.println("结束卖票"+Thread.currentThread().getName()); } class Ticket implements Runnable{ private int num=100; @Override public void run() { sale(); } private void sale(){ while (true){ if(num>0){ System.out.println("正在卖第"+num+"张票"+Thread.currentThread().getName()); num--; } } } }
标签:rri close span current nbsp java pen 多线程 runnable
原文地址:http://www.cnblogs.com/caohuimingfa/p/7818797.html