Concurrent package is commonly used multi-threaded related packages, recently because of the development of SDN program, for multi-threaded use more than before, is a simple summary.
The first class of atomic class: a situation in which multiple threads work together to manipulate a count
Atomiclong
Atomicinteger
Type II Lock and condition
Condition is obtained from lock, so in use, after the execution of Lock.lock () condition operation, condition commonly used two methods await and signal.
It is common to use multiple threads to manipulate a common resource, and one thread can execute a situation after the end of execution by another thread.
In addition, lock should replace the previous synchronized keyword. The synchronized is a synchronization policy at the JVM level that is allocated and freed by the JVM for locks. However, it is said that high concurrency requires frequent switching of line stacks and poor performance.
From now on, you should use lock for synchronization in your code.
The third kind of multi-threaded task execution Executorservice
This section is largely about three concepts,
Callable executed task executor perform task () The return data of the future asynchronous commit task Futuretask for the specific implementation
Thread resources are precious resources of the system, and indeed it should not be the programmer's random new thread way to start the thread themselves. If the project has its own framework, you should use the threading tools in the project framework if there is no best use of the Executorservice tool class provided by the JDK.
Common thread pool
- Executorservice Cachedthreadpool = Executors.newcachedthreadpool (); --Timeout destroys the thread in the pool
- Executorservice Fixedthreadpool = Executors.newfixedthreadpool (3); --Threads in the thread pool are not destroyed even when they are idle
- Executorservice Singlethreadpool = Executors.newsinglethreadexecutor ();
- Scheduledexecutorservice Scheduledthreadpool = Executors.newscheduledthreadpool (3)--a thread pool capable of performing tasks on a timed basis, For example, can be used in the data collection function, every hour to execute
Class Fourth Tool class
such as Cyclicbarrier, Countdownlatch is used to coordinate multiple threads, execute sequential
Class fifth thread-safe collection classes such as Blockingqueue Concurrentmap
Java Concurrent Package Common class summary