同步代码块
synchronized (this){
if (this.ticket > 0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +
" ticket " + this.ticket--);
}
}
同步方法
public synchronized void sale(){
if (this.ticket > 0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +
" ticket " + this.ticket--);
}
}