package com.heima.demo;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
MaiPiao mp = new MaiPiao();
new Thread(mp,"窗口a").start();
new Thread(mp,"窗口b").start();
}
}
class MaiPiao implements Runnable {
private static int piao = 100;
public void run() {
int count = 0;
while(true){
String line = Thread.currentThread().getName();
synchronized (MaiPiao.class) {
// 票没了退出
if(piao<=0){
break;
}
try {
// 每300毫秒卖一张票
Thread.sleep(300);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
// 指定窗口卖第几张票
if(piao==19&&"窗口a".equals(line)){
continue;
}
System.out.println(Thread.currentThread().getName() + "卖了第" + (100- --piao) + "张,还剩"+ piao );
count++;
}
}
//各个窗口分别卖了多少张票
System.out.println(Thread.currentThread().getName()+"总共卖出"+count+"个");
}
}
|
|