1. 程式人生 > 實用技巧 >聊聊java多執行緒(轉載)

聊聊java多執行緒(轉載)

本篇文章,為本人在某一階段對此問題的個人理解總結,水平有限,會不斷更新。

對java中執行緒同步的理解

對於這個話題,我們可以從兩個方面去拆解,思考,

首先,我需要知道執行緒同步的概念,什麼是執行緒同步呢?為了充分利用硬體資源(對應多cpu單核、單cup多核、多cpu多核)去更高效的去完成一項工作內容,通常,我們將一份整體的工作內容拆解成可並行執行的多個單一工作任務,然後,啟用多個執行緒(t1、t2...tn)去執行拆解後的每一個工作任務,以達成最終的目標。在整個執行過程中,多個執行緒之間免不了在某些個執行處交叉關聯,例如,多個執行緒同時讀寫同一塊記憶體地址,對於每個執行緒來說,對這塊記憶體地址的操作必須是原子操作,並且一定的操作次序。這樣,採用有效的方法,保證讓多個(大於等於2個執行緒)參與執行緒按照正確的執行次序,正確的執行行為,一起正確的完成整項工作內容的執行約束,稱之為執行緒同步。所以,這裡說的同步,就是程式按照預定的先後次序執行。同,指的是協同、協助、互相配合。

其次,在java中怎麼實現執行緒同步,首先需要明確什麼時候需要同步,借用一下Brian’s Rule of Synchronization,

If you are writing a variable that might next be read by another thread,or reading a variable that might have last been written by another thread,you must use synchronization,and further,both the reader and the writer must synchronize using the same monitor lock.

  1. 在特定的場景下的特定資源,可以使用volatile保證資料的可見性、Atomic classes來實現資源的原子性操作,Thread local storage來避免共享資源的衝突,達到一定程度的同步效果;

  2. 如果你足夠expert,你可以不依賴於任何的工具包自行實現同步編碼,自己編寫lock-free程式碼;
  3. 使用併發程式設計工具,
  • java jdk本身提供的工具,java.util.concurrent.*,高階併發程式設計構件(CountDownLatch, CyclicBarrier, DelayQueue, PriorityBlockingQueue, Exchanger, Producer-consumers and queues:ArrayBlockingQueue, LinkedBlockingQueue),Cooperation methods, 同步程式碼塊,同步方法,互斥器,條件變數,Semaphore,Atomic classes等;
  • 其他工具庫,例如,google 的guava;

接下來,學習一下java裡的Thread state,

public enum State {
    /**
     * Thread state for a thread which has not yet started.
     */
    NEW,

    /**
     * Thread state for a runnable thread.  A thread in the runnable
     * state is executing in the Java virtual machine but it may
     * be waiting for other resources from the operating system
     * such as processor.
     */
    RUNNABLE,

    /**
     * Thread state for a thread blocked waiting for a monitor lock.
     * A thread in the blocked state is waiting for a monitor lock
     * to enter a synchronized block/method or
     * reenter a synchronized block/method after calling
     * {@link Object#wait() Object.wait}.
     */
    BLOCKED,

    /**
     * Thread state for a waiting thread.
     * A thread is in the waiting state due to calling one of the
     * following methods:
     * <ul>
     *   <li>{@link Object#wait() Object.wait} with no timeout</li>
     *   <li>{@link #join() Thread.join} with no timeout</li>
     *   <li>{@link LockSupport#park() LockSupport.park}</li>
     * </ul>
     *
     * <p>A thread in the waiting state is waiting for another thread to
     * perform a particular action.
     *
     * For example, a thread that has called <tt>Object.wait()</tt>
     * on an object is waiting for another thread to call
     * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
     * that object. A thread that has called <tt>Thread.join()</tt>
     * is waiting for a specified thread to terminate.
     */
    WAITING,

    /**
     * Thread state for a waiting thread with a specified waiting time.
     * A thread is in the timed waiting state due to calling one of
     * the following methods with a specified positive waiting time:
     * <ul>
     *   <li>{@link #sleep Thread.sleep}</li>
     *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
     *   <li>{@link #join(long) Thread.join} with timeout</li>
     *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
     *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
     * </ul>
     */
    TIMED_WAITING,

    /**
     * Thread state for a terminated thread.
     * The thread has completed execution.
     */
    TERMINATED;
}

待續。。。