//使用同步方法
publicclass Test5{
publicstaticvoid main(String[] args) {
MyThread mt = new MyThread();
new Thread(mt,"A").start();
new Thread(mt,"B").start();
new Thread(mt,"C").start();
}
}
class MyThread implements Runnable{
privateintticket = 10;
@Override
publicvoid run() {
for(inti = 0 ; i < 20 ; i++){
this.sale();
}
}
public synchronizedvoid sale(){ 同步方法
if(this.ticket > 0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+ "saleticket=" + this.ticket--);
}
}
}