车站三个窗口买票
方法一:通过Thread接口进行多线程
class Window4 extends Thread {
private static int ticket = 100;
@Override
public void run() {
while (true) {
show();
}
}
private static synchronized void show(){
//同步监视器:Window4.class
//private synchronized void show(){ //同步监视器:t1,t2,t3。此种解决方式是错误的
if (ticket > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);
ticket--;
}
}
}
public class WindowsTest {
public static void main(String[] args) {
Window4 t1 = new Window4();
Window4 t2 = new Window4();
Window4 t3 = new Window4();
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t3.start();
t2.start();
t1.start();
}
}
结果:
方法二继承Runable接口:
class Window3 implements Runnable {
private static int ticket = 100;
@Override
public void run() {
while (ticket>0) {
show();
}
}
private synchronized void show(){
//同步监视器:this
//synchronized (this){
if (ticket > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
ticket--;
System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);
}
//}
}
}
public class WindowsTest3 {
public static void main(String[] args) {
Window3 w = new Window3();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
结果:
总结
无论哪种方法实现都需要进行synchronized进行修饰,这是为了保护线程安全,不然会同时涌入多个线程,还有用Thread接口继承的时候,我们需要将show方法声明为static静态方法,不然new三个对象,会创建三个方法,还是会发生多线程涌入。