聊聊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 threador reading a variable that might have last been written by another threadyou must use synchronizationand furtherboth 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:
     * 
    *
  • {@link Object#wait() Object.wait} with no timeout
  • *
  • {@link #join() Thread.join} with no timeout
  • *
  • {@link LockSupport#park() LockSupport.park}
  • *
* *

A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called Thread.join() * 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: *

    *
  • {@link #sleep Thread.sleep}
  • *
  • {@link Object#wait(long) Object.wait} with timeout
  • *
  • {@link #join(long) Thread.join} with timeout
  • *
  • {@link LockSupport#parkNanos LockSupport.parkNanos}
  • *
  • {@link LockSupport#parkUntil LockSupport.parkUntil}
  • *
*/ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }

待续。。。

你可能感兴趣的:(聊聊java多线程(转载))