首页 > 代码库 > 多线程与静态代理

多线程与静态代理

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抢到了48
hong抢到了47
hong抢到了46
hong抢到了45
hong抢到了44
hong抢到了43
hong抢到了42
hong抢到了41
hong抢到了40
hong抢到了39
hong抢到了37
hong抢到了36
hong抢到了35
hong抢到了34
hong抢到了33
hong抢到了32
hong抢到了31
xiaoming抢到了49
hong抢到了30
huahua抢到了38
hong抢到了28
xiaoming抢到了29
hong抢到了26
huahua抢到了27
hong抢到了24
xiaoming抢到了25
xiaoming抢到了21
xiaoming抢到了20
xiaoming抢到了19
xiaoming抢到了18
hong抢到了22
hong抢到了16
huahua抢到了23
hong抢到了15
xiaoming抢到了17
hong抢到了13
huahua抢到了14
hong抢到了11
xiaoming抢到了12
hong抢到了9
huahua抢到了10
hong抢到了7
xiaoming抢到了8
hong抢到了5
huahua抢到了6
hong抢到了3
xiaoming抢到了4
hong抢到了1
huahua抢到了2

多线程与静态代理