天天看點

java多線程售票小例子

java多線程售票小例子

1、錯誤示範

class SellThread implements Runnable {
    private int i = 20;

    public void run() {
        while(true) {
            if(i > 0) {
                try {
                    Thread.sleep(100);
                } catch(Exception e) {

                }
                System.out.println(Thread.currentThread().getName() + "sell " + i--);   
            }
        }
    }
}

public class Test

    public static void main(String[] args) {
        SellThread sell = new SellThread();
        Thread sell1 = new Thread(sell, "sellman1");
        Thread sell2 = new Thread(sell, "sellman2");
        Thread sell3 = new Thread(sell, "sellman3");
        sell1.start();
        sell2.start();      
        sell3.start();
    }
}      

沒有同步控制變量,是以最後出現了-1張票

java多線程售票小例子

2、正确示範

class SellThread implements Runnable {
    private int i = 20;
    String key = "";

    public void run() {
        while(true) {
            synchronized(key) {
                if(i > 0) {
                    try {
                    Thread.sleep(100);
                    } catch(Exception e) {

                    }
                    System.out.println(Thread.currentThread().getName() + "sell " + i--);   
                }else {
                    break;
                }
            }
        }
    }
}

public class Test

    public static void main(String[] args) {
        SellThread sell = new SellThread();
        Thread sell1 = new Thread(sell, "sellman1");
        Thread sell2 = new Thread(sell, "sellman2");
        Thread sell3 = new Thread(sell, "sellman3");
        sell1.start();
        sell2.start();      
        sell3.start();
    }
}      
java多線程售票小例子

補充:如果用synchronized關鍵字修飾方法,則隻能有一個線程擷取通路這段方法的權限。也就是說,一個線程把票買完了才結束。

class SellThread implements Runnable {
    private int i = 20;
    String key = "";

    public synchronized void run() {
        while(true) {
            if(i > 0) {
                try {
                    Thread.sleep(100);
                    } catch(Exception e) {

                    }
                    System.out.println(Thread.currentThread().getName() + "sell " + i--);                       
            }else {
                break;
            }
        }
    }
}

public class Test

    public static void main(String[] args) {
        SellThread sell = new SellThread();
        Thread sell1 = new Thread(sell, "sellman1");
        Thread sell2 = new Thread(sell, "sellman2");
        Thread sell3 = new Thread(sell, "sellman3");
        sell1.start();
        sell2.start();      
        sell3.start();
    }
}      
java多線程售票小例子

總結:

1、synchronized()括号中需要的是一個對象,是以這裡使用了String類的一個對象key,可以用this來代替key。

2、一個線程拿到synchronized括号中的對象以後,其他線程也是可以執行的,但是當它們需要用key的時候,發現key已經被别人拿走了,隻能等着key被釋放了。就像上廁所,坑被别人占了,隻能等着了,但是等着的時候我還可以運作,比如玩玩手機什麼的。

3、synchronized method(){} 可以防止多個線程同時通路這個對象的synchronized方法;如果一個對象A,有多個synchronized方法,隻要一個線程通路了其中的一個,那麼其他線程不能同時通路A的任何一個synchronized方法。但是可以通路對象B的synchronized方法。

繼續閱讀