Java 1.5 before
Concurrent achieve
- Java Green Thread
java 1.2 before the thread by os kernel restrictions, thread = process, green thread JVM is scheduled to simulate a multi-threaded environment No native thread support.
- Java Native Thread
Comparative
green threads in the thread activation than native threads and thread synchronization aspects of
the I / O performance and operational aspects of the context of the thread to be lower than the local
Programming Model
- Thread
- Runnable
Limitations (Future releases will resolve to improve this part of the defect, comparable to learn)
- The lack of native support for thread management (lack of a thread pool)
- Lack of "lock" API (only synchronized keyword)
- The lack of native support for implementation of the completion of
Alternative ways:
public static void main(String[] args){
CompletableRunnable runnable = new CompletableRunnable();
Thread thread = new Thread(runnable,"Sub");
thread.start();
thread.join(); //此处阻塞主线程等待Sub完成, 否则得到的结果不准确
System.out.printf("[Thread: %s] is running\n", Thread.currentThread.getName());
System.out.printf("Sub Thread is completed, result is %s\n", runnable.getCompleted());
}
private static class CompletableRunnable implements Runnable{
private volatile boolean completed = false;
@Override
public void run(){
System.out.printf("[Thread: %s] is running\n", Thread.currentThread.getName());
completed = true;
}
public boolean getCompleted(){
return completed;
}
}
- The difficulty of obtaining the results
- Double Check Locking uncertainty (singleton)
Java 5
Concurrency Framework
- J.U.C
Programming Model
- Executor, Execurtors, ExecutorService 等
- Runnable , Callable
- Future
Java 7
Parallel Framework
- Fork/Join
Programming Model
- ForkJoinPool
- ForkJoinTask
- RecursiveAction
Future (Java 5 introduced) limit
- Can not be done by hand
- Blocking results returned
- Unable to chain multiple Future
- Future results can not merge multiple
- The lack exception handling
Java 8
Asynchronous parallel frame
- Fork/Join
Programming Model
- CompletionStage
- CompletableFuture
Other frameworks
- Spring and Guava use different
ListenableFuture