package com.atguigu.java;
/**
*使用同步方法
*
* @author Dxkstart
* @create 2021-05-07 18:16
*/
public class WindowTest5 {
public static void main(String[] args) {
Window5 w1 = new Window5();
Window5 w2 = new Window5();
Window5 w3 = new Window5();
w1.start();
w2.start();
w3.start();
}
}
class Window5 extends Thread {
private static int ticket = 100;//票数
@Override
public void run() {
while(true){
show();
}
}
public static synchronized void show(){//同步监视器:Window5.class
// public synchronized void show(){//同步监视器:w1、w2、w3,此种解决方法时错误的
if(ticket > 0){
try {
sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": 卖票,票号为" + ticket);
ticket --;
}
}
}