This article analyzes the use and concurrent usage in Java in detail. Share it for everyone for your reference. The specific analysis is as follows:
1. The synchronous container includes two parts: vector and hashtable
The other is a synchronous packaging class, which was created by Collections. SynchronizedXXX. The synchronous container interviews all states of the container to achieve thread safety.
They have the following problems:
a) For operation, additional lock protection is required. For example, iterative, missing conditions such as adding.
b) Tostring, HashCode, Equals will indirectly call iterative, and you need to pay attention to concurrency.
2. concurrent container in Java5.0.
ConcurrenThashMap can replace synchronous MAP implementation.
When it is mainly read, CopyonWritearrayList is a synchronous implementation of List.
At the same time, Queue and BlockingQueue: ConcurentlinkedQueue: FIFO queue.
PriorityQueue: non -merger priority queue.
BlockingQueue increased blocking and acquisition of blocking.
a) Concurenthashmap: It is not a public lock, but a separate lock. Any number of reading threads can be accessed concurrently, readers and writers can also access MAP concurrently, and the limited number of writing can also modify the MAP concurrently. However, the size and ISEMPTY language is weakened, which is estimated.
b) CopyonWritearrayList: The iterator retains a bottom -layer base array reference, and the reference will not be modified.
3. Synchronizer
a) Countdownlatch. The constructor parameter is n, after countdown, n is 0, and all AWAIT threads continue to execute.
b) FUTURETASK: Calculate Runnable, which can be implemented by Callable and can carry the result. Including: wait, run and complete.
c) Semaphore: The number of activities that can be accessed at the same time
d) CyclicBarrier: Similar to atresia. The lock is waiting for the incident, and the level is waiting for other threads. Initial N, Runnable R, n threads are AWAIT (). When the n threads are executed to reach the level, R execute, the level is reset, and the n threads continue.
It is hoped that this article is helpful to everyone's Java program design.