Java 并发

并发博客

BlockingQueue

高并发

什么是Java并发

广义地讲,可以认为是JVM平台上的并发。如果是这样定义Java并发,那么,一下几个方面都算是Java并发的范畴:

  • JDK中的Java并发: Thread, ExectutorService, ForkJoinPool, ParrallelStream.
  • 第三方库或者框架的并发: Vert.x, RxJava
  • 其他,JVM base的编程语言: Akka

因为多线程只是实现并发的一种方式,像Akka, Vert.x, RxJava中。

提高并发的途径和方式:

考虑并发的基本角度:

  • CPU: CPU密集,CPU切换
  • IO: IO密集, IO阻塞

并发概念

并发和多线程概念的区别

多线程只是实现并发的一种模型或者技术。参见:http://tutorials.jenkov.com/java-concurrency/concurrency-models.html

并发基础概念

管程(Mointor): 不是很懂,需理解:

Java 并发的基本概念

Java线程的生命周期

(通读Thread Javadoc: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

Java线程的状态只包含NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED. 没有Running状态

  • 线程的创建与销毁

From JDK javadoc:( https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html

public static enum Thread.State
extends Enum<Thread.State>
A thread state. A thread can be in one of the following states:

NEW
A thread that has not yet started is in this state.

RUNNABLE
A thread executing in the Java virtual machine is in this state.

BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.

WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.

TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.

TERMINATED
A thread that has exited is in this state.

A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.

下面的几张线程状态转移图的描述也不准确, 比较着看:(后面两张图是来自上面两个StackOverflow链接的)

Java线程的状态值代表JVM层面的状态,和操作系统线程的状态没有直接对应关系。

Java并发基本工具

Java 并发的底层工具:AQS

<Java并发编程实战>说可以不了解那么深入。在ImportNews看过一些文章,够了。

先从概念上理解下,有空闲时间,在去看实现机制。

Core Java 并发

https://dzone.com/refcardz/core-java-concurrency

概念

Java内存模型, 监视器锁,原子地变量赋值,Race condition,Data race,安全的发布,Final Field,不可变对象

保护共享的数据

Synchronized, Lock, ReadWriteLock, Volatile, Atomic Class, ThreadLocal

并发集合数据结构, 共享的数据在集合里

Concurrent List and Set: CopyOnWriteArraySet, CopyOnWriteArrayList, ConcurrentSkipListSet

Concurrent Map: ConcurrentHashMap, ConcurrentSkipListMap

Queues: PriorityQueue, ConcurrentLinkedQueue, ArrayBlockingQueue, LinkedBlockingQueue, ProrityBlockingQueue,

DelayQueue, SynchronousQueue

Deque: LinkedList, ArrayDeque, LinkedBlockingQueue.

线程

线程生命周期

线程通信:最简单直白的方法就是调用对应线程上的方法 : thread.start, thread.join, thread.interrupt, ...

线程协调同步:wait/notify, condition, 协调类: CyclicBarrier, CountDownLatch, Semaphore, Exchanger.

任务执行:ExectuorService, CompletionService(如何取结果)

Exectuor框架

核心类:

基本功能:

  • 提交任务:submit, execute
  • 获取任务状态和结果,Future
  • 取消任务: Future.cancel
  • 定时任务: Future.get(timeout, unit)

并发Collection

Java原子类型

Java 内存模型:

Java并发的常见问题

http://mp.weixin.qq.com/s/RJdncVqczhtnGE7OSg0aKA

我的答案

CyclicBarrier和CountDownLatch有什么不同?

  • CyclicBarrier相关的线程之间没有主次关系,CyclicBarrier可以重复使用

References:

results matching ""

    No results matching ""