使用Runnable實現數據共享,供大家參考,具體內容如下
先上代碼:
public class TestThread { private static final Logger logger = LoggerFactory.getLogger(TestThread.class); private final class MyRunnable implements Runnable { private int i; public MyRunnable() { this.i = 10; } public void run() { while(i > 0) { synchronized (this) { if (i > 0) { i--; logger.debug("{} buy one ticket, {} left. ", Thread.currentThread().getName(), i); } } } } } @Test public void testRunable() throws InterruptedException{ MyRunnable myRunnable = new MyRunnable(); Thread th1 = new Thread(myRunnable); Thread th2 = new Thread(myRunnable); th1.start(); th2.start(); th1.join(); th2.join(); }}
樓上的代碼很簡單,模擬一個售票系統。通過兩個Thread對象開啟兩條線程同時運行一個MyRunnable實例。
幾個注意點:
1. 沒有加上synchronised關鍵詞的話,即
public void run() { while(i > 0) { if (i > 0) { i--; logger.debug("{} buy one ticket, {} left. ", Thread.currentThread().getName(), i); } } }
系統的運行結果:
Thread-1 buy one ticket, 8 left. Thread-2 buy one ticket, 8 left. Thread-2 buy one ticket, 6 left. Thread-1 buy one ticket, 6 left. Thread-2 buy one ticket, 5 left. Thread-1 buy one ticket, 4 left. Thread-2 buy one ticket, 3 left. Thread-1 buy one ticket, 2 left. Thread-2 buy one ticket, 1 left. Thread-1 buy one ticket, 0 left.
可以看到,缺少同步的程序輸出明顯有問題。
2. 在進入同步代碼塊之后,還需要對i的值再進行一次判斷,即,如果不加if判斷:
public void run() { while(i > 0) { synchronized (this) { i--; logger.debug("{} buy one ticket, {} left. ", Thread.currentThread().getName(), i); } } }
程序的運行結果為:
Thread-2 buy one ticket, 9 left. Thread-2 buy one ticket, 8 left. Thread-2 buy one ticket, 7 left. Thread-2 buy one ticket, 6 left. Thread-2 buy one ticket, 5 left. Thread-2 buy one ticket, 4 left. Thread-2 buy one ticket, 3 left. Thread-2 buy one ticket, 2 left. Thread-2 buy one ticket, 1 left. Thread-2 buy one ticket, 0 left. Thread-1 buy one ticket, -1 left.
可以看出,出現了“多賣”的現象, 所以需要在進入同步代碼塊中再進行一次if判斷。
總結
synchronised用于互斥訪問共享變量, 并在同步代碼塊中使用if判斷更新共享變量。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選