class Xc8 implements Runnable {
public static int chepiao = 100;
String ax = new String("1");
public void run() {
while (true) {
// synchronized既可以修饰代码块,有可以修饰函数,修饰函数不用参数
synchronized (ax) {
if (chepiao > 0) {
System.out.println("第" + Thread.currentThread().getName()
+ "个车站正在卖第" + (101 - chepiao) + "张车票");
--chepiao;
} else {
break;
}
}
}
}
}
public class Lab7 {
public static void main(String[] args) {
Xc8 xx = new Xc8();
Thread aa = new Thread(xx);
aa.start();
Xc8 yy = new Xc8();
Thread bb = new Thread(yy);
bb.start();
}
}