package com.heima;
public class Test12 {
/**
*
一共100盒盒饭,2个窗口领取,每取一次间隔30毫秒,线程打印出取饭过程和剩余饭盒数;
*/
public static void main(String[] args) {
HeFan hf1 = new HeFan("一号窗口");
HeFan hf2 = new HeFan("二号窗口");
hf1.start();
hf2.start();
}
}
class HeFan extends Thread{
private static int num = 100;
public HeFan() {
super();
}
public HeFan(String name) {
super(name);
}
@Override
public void run() {
while(true){
synchronized (HeFan.class) {
if(num <= 0){
break;
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName()+"取了一盒盒饭,还剩"+--num+"盒");
}
}
}
} |