package thread;//方便共享资源public class 多线程与代理 { public static void main(String[] args) { //真实角色 Web12306 web=new Web12306(); //代理 Thread t1=new Thread(web,"xiaoming"); Thread t2=new Thread(web,"hong"); Thread t3=new Thread(web,"huahua"); t1.start(); t2.start(); t3.start(); }}class Web12306 implements Runnable{ private int num=50;//剩余50张票 @Override public void run() { // TODO Auto-generated method stub while(true){ if(num<=0){ break;//跳出循环 } System.out.println(Thread.currentThread().getName()+"抢到了"+num--); } } }
hong抢到了50
hong抢到了48hong抢到了47hong抢到了46hong抢到了45hong抢到了44hong抢到了43hong抢到了42hong抢到了41hong抢到了40hong抢到了39hong抢到了37hong抢到了36hong抢到了35hong抢到了34hong抢到了33hong抢到了32hong抢到了31xiaoming抢到了49hong抢到了30huahua抢到了38hong抢到了28xiaoming抢到了29hong抢到了26huahua抢到了27hong抢到了24xiaoming抢到了25xiaoming抢到了21xiaoming抢到了20xiaoming抢到了19xiaoming抢到了18hong抢到了22hong抢到了16huahua抢到了23hong抢到了15xiaoming抢到了17hong抢到了13huahua抢到了14hong抢到了11xiaoming抢到了12hong抢到了9huahua抢到了10hong抢到了7xiaoming抢到了8hong抢到了5huahua抢到了6hong抢到了3xiaoming抢到了4hong抢到了1huahua抢到了2