package com.homework.demo;
class Test extends Thread{
int num = 100;
Object lock = new Object();
public void run(){
while(true){
synchronized(lock){
if(num > 0){
System.out.println(Thread.currentThread().getName()+"出售编号为"+num+"的票");
num--;
}
}
}
}
}
public class HomeWork2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t1 = new Test();
t1.setName("窗口1");
t1.start();
Test t2 = new Test();
t2.setName("窗口2");
t2.start();
Test t3 = new Test();
t3.setName("窗口3");
t3.start();
}
}
|
|