public class TongBu implements Runnable {
private int tickets = 10;
Object lock = new Object();
public void run() {
// TODO Auto-generated method stub
while(true)
{
synchronized (lock) {
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if(tickets>0)
{
System.out.println(Thread.currentThread().getName()+"卖出了的票"+tickets--);
}
else
break;
}
}
}
}
而且为什么在run()方法里不能用抛出声明? |
|