Java 并发
并发博客
BlockingQueue
- http://www.jianshu.com/p/5dfc81b10e30
ConccurentHashMap
- TODO
高并发
- https://www.zhihu.com/question/40609661 (如何获得高并发经验)
- https://book.douban.com/subject/26663605/ (Java并发,书的结构还可以,评价一般,粗糙)可以借鉴结构, 进行梳理。
- http://www.jianshu.com/p/cf856b94f877 (网站层面的并发)
什么是Java并发
广义地讲,可以认为是JVM平台上的并发。如果是这样定义Java并发,那么,一下几个方面都算是Java并发的范畴:
- JDK中的Java并发: Thread, ExectutorService, ForkJoinPool, ParrallelStream.
- 第三方库或者框架的并发: Vert.x, RxJava
- 其他,JVM base的编程语言: Akka
因为多线程只是实现并发的一种方式,像Akka, Vert.x, RxJava中。
提高并发的途径和方式:
- 多进程
- 多线程
- 协程 (https://www.zhihu.com/question/32218874 纤程库Quasar
- 响应式
考虑并发的基本角度:
- CPU: CPU密集,CPU切换
- IO: IO密集, IO阻塞
并发概念
并发和多线程概念的区别
多线程只是实现并发的一种模型或者技术。参见:http://tutorials.jenkov.com/java-concurrency/concurrency-models.html
并发基础概念
管程(Mointor): 不是很懂,需理解:
- https://objectkuan.gitbooks.io/ucore-docs/lab7/lab7_3_4_monitors.html
- http://ifeve.com/monitors-java-synchronization-mechanism/
- http://blog.forec.cn/2016/11/24/os-concepts-6/ (四星资料)
Java 并发的基本概念
线程的生命周期(New, Runnable, Running,
线程封闭?
锁
Runnable和Callable
Java内存模型
阻塞
竞争条件(Race Condition)和数据竞争(Data Race): http://stackoverflow.com/questions/11276259/are-data-races-and-race-condition-actually-the-same-thing-in-context-of-conc
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.
调用Thread.sleep, 会使线程进入TIMED_WAITING状态。
线程状态
下面的几张线程状态转移图的描述也不准确, 比较着看:(后面两张图是来自上面两个StackOverflow链接的)


Java线程的状态值代表JVM层面的状态,和操作系统线程的状态没有直接对应关系。
- http://beginnersbook.com/2013/03/thread-life-cycle-in-java/
- https://www.javacodegeeks.com/2012/08/java-thread-at-runnable-state-is-not.html
- https://coderanch.com/t/509637/certification/Thread-RUNNABLE-RUNNING
Java并发基本工具
- 原子类型
- 并发Collection: ConcurrentMap, CopyOnWriteList
- 线程运行、调度与管理框架: Executor
- Volatile.
- https://dzone.com/refcardz/core-java-concurrency 非常详细和结构化
Java 并发的底层工具:AQS
<Java并发编程实战>说可以不了解那么深入。在ImportNews看过一些文章,够了。
- http://gee.cs.oswego.edu/dl/papers/aqs.pdf
- http://www.javarticles.com/2012/10/abstractqueuedsynchronizer-aqs.html
先从概念上理解下,有空闲时间,在去看实现机制。
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框架
核心类:
- ThreadPoolExecutor: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html
基本功能:
- 提交任务:submit, execute
- 获取任务状态和结果,Future
- 取消任务: Future.cancel
- 定时任务: Future.get(timeout, unit)

并发Collection

Java原子类型
Java 内存模型:
- http://tutorials.jenkov.com/java-concurrency/java-memory-model.html
- http://www.cs.umd.edu/~pugh/java/memoryModel/Dagstuhl.pdf
- http://www.infoq.com/cn/articles/java-memory-model-1
- http://www.cs.umd.edu/~pugh/java/memoryModel/CommunityReview.pdf
- http://rsim.cs.uiuc.edu/Pubs/popl05.pdf
- http://www.uml-diagrams.org/java-7-concurrent-uml-class-diagram-example.html
Java并发的常见问题
http://mp.weixin.qq.com/s/RJdncVqczhtnGE7OSg0aKA
- Thread的run和start方法的区别
- Runnable与Callable的区别
Java中CyclicBarrier 和 CountDownLatch有什么不同?
如何记录几个线程的执行结果或状态?
Java内存模型是什么?
Java中什么是竞态条件? 举个例子说明。
Java中如何停止一个线程? 线程结束的几种可能情形, run方法中的逻辑执行完毕,run方法的执行抛出了异常, 线程调用了可中断方法,并被中断:run方法中调用了Thread.sleep. Thread.join.
一个线程运行时发生异常会怎样?
如何在两个线程间共享数据?
Java线程池中submit() 和 execute()方法有什么区别? http://stackoverflow.com/questions/3929342/choose-between-executorservices-submit-and-executorservices-execute submit 默认情况下,不会调用Thread的默认UncaughtExceptionHandler:打印stacktrace到System.err; 而execute会。
在使用ExecutorSevice时,如何处理RuntimeException or UncaughtException?
- sumbit和Future
, 使用Future.get, 会抛出ExecutionExeeption. 调用.getCause去获得线程抛出的异常 - 使用ThreadFactory, 在创建线程时,调用Thread.setUncaughtExceptionHandler,去注册未捕获异常的处理逻辑
- 继承ThreadPoolExecutor, 并重写afterExecute(Runnable r, Throwable t), t就是对应的为捕获异常,可以在此处处理
- https://stackoverflow.com/questions/4492273/catching-thread-exceptions-from-java-executorservice
- https://www.securecoding.cert.org/confluence/display/java/TPS03-J.+Ensure+that+tasks+executing+in+a+thread+pool+do+not+fail+silently
- http://www.nurkiewicz.com/2014/11/executorservice-10-tips-and-tricks.html
- http://blog.kavinyao.com/2014/11/java-thread-pool/
- sumbit和Future
我的答案
CyclicBarrier和CountDownLatch有什么不同?
- CyclicBarrier相关的线程之间没有主次关系,CyclicBarrier可以重复使用