public class Thread2 {
public static void main(String[] args) {
ShareData d = new ShareData();
//两个窗口卖票
new Thread(d).start();
new Thread(d).start();
}
}
class ShareData implements Runnable {
private int count = 100;
@Override
public void run() {
synchronized (ShareData.class) {
while (count > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().getName() + "余票: " + count);
}
}
}
}