Software Package java. util. Concurrent
A common Utility Class for concurrent programming.
Description of the software package java. util. Concurrent
A common Utility Class for concurrent programming. This package includes several small, standardized and scalable frameworks, and some classes that provide useful functions.
. The main components are briefly described below. For more information, seeLocksAndAtomicPackage.
Interface
ExecutorIs a simple standardized interface used to define custom subsystems similar to threads, including thread pools, asynchronous Io, and lightweight task frameworks. Root
Depending on the specific executor class used, it may be in the newly created thread, the existing task execution thread, orExecute ()In the thread
Execute tasks in sequence or concurrently.ExecutorServiceProvides multiple complete asynchronous task execution frameworks. Executorservice Management
Tasks are queued and arranged, and controlled shutdown is allowed.ScheduledExecutorServiceAdded latency and scheduled tasks for sub-interfaces and related interfaces.
Service Execution Support. Executorservice provides a method for arranging asynchronous execution.
CallableAny function. The result is similar
Runnable.FutureReturns the result of the function, allows you to determine whether the execution is complete, and provides a method to cancel the execution.RunnableFuture
YesRun
Method future,RunThe result is set when the method is executed.
Implementation
Class
ThreadPoolExecutorAnd
ScheduledThreadPoolExecutorProvides an adjustable and flexible thread pool.ExecutorsMost classes are provided
Common types and configuration factory methods of executors, and several utility methods for using them. Other executor-based utilities include
Body Type
FutureTask, Which provides common extensible implementations of the future, and
ExecutorCompletionServiceIt helps to coordinate asynchronous tasks
The processing of the Service Group.
QueueJava. util. Concurrent
ConcurrentLinkedQueueClass provides efficient, scalable, and thread-safe non-blocking FIFO queues.
The five implementations in Java. util. Concurrent support extended
BlockingQueueInterface, which defines the blocking versions of put and take:
LinkedBlockingQueue,ArrayBlockingQueue,SynchronousQueue,PriorityBlockingQueue
And
DelayQueue. These classes cover most of the Common Use Cases of producer-user, message passing, parallel task execution, and related concurrency design.
Text.BlockingDequeInterface Extension
BlockingqueueTo support FIFO and LIFO (stack-based) operations.LinkedBlockingDeque
Class provides an implementation.
TimingTimeUnitClass provides multiple granularities (including nanoseconds) for specifying and controlling timeout-based operations ). Most classes in this package contain uncertain waits
Besides, timeout-based operations are also included. In all cases where timeout is used, timeout specifies the minimum time that the method should wait before timeout. Sending upon timeout
After being generated, the implementation will "try its best" to detect timeout. However, it may take an uncertain time between the detection timeout and the actual execution thread again after the timeout. Accept timeout
All methods of phase parameters are considered as not waiting for values smaller than or equal to 0. To "always" wait, you can useLong. max_valueValue.
SynxThe four types can help implement common dedicated synchronization statements.SemaphoreIs a classic concurrency tool.CountDownLatch
Is extremely simple but extremely
A common utility used to block execution before a specified number of signals, events, or conditions are maintained.CyclicBarrierIs a reset multi-channel synchronization point, in
Some parallel programming styles are useful.ExchangerTwo threads are allowed to exchange objects at collection points, which is useful in Multi-pipeline design.
Concurrent collectionIn addition to queues, this package also provides collection implementation designed for multi-threaded context:ConcurrentHashMap,
ConcurrentSkipListMap,ConcurrentSkipListSet,CopyOnWriteArrayList
And
CopyOnWriteArraySet. Current Period
When many threads access a given collection,ConcurrenthashmapUsually better than synchronousHashmap,Concurrentskiplistmap
Usually better than synchronousTreemap. When the expected reading and traversal are much larger than the number of list updates,CopyonwritearraylistBetter than synchronous
Arraylist.
The "concurrent & rdquo prefix; used with some classes in this package is a short form, indicating that it is different from a similar" synchronization "class. For example,
Java. util. hashtableAndCollections. synchronizedmap (New hashmap ())Yes,
ConcurrentHashMap
It is "concurrent ". Concurrent collection is thread-safe, but is not managed by a single exclusive lock. In the specific situation of concurrenthashmap
It can securely allow any number of concurrent reads and write with an adjustable number. You must use a single lock to disallow all
During access, the "synchronization" class is very useful, and its cost is poor scalability. In other cases where multiple threads are expected to access the public collection
"Concurrent" versions should be better. If the collection is not shared, or the collection is accessible only when other locks are kept, it is not synchronized.
Collection is better.
Most concurrent collection implementations (including most Queue) are different from conventional java. util conventions because their iterators provideWeak ConsistentInstead of traversing a fast failure. The Weak Consistent iterator is thread-safe, but there is no need to freeze the collection during iteration, so it does not necessarily reflect all updates since the iterator was created.
Java language specification chapter 17th defines
Happen-beforeLink. Only write operationsHappen-beforeRead Operations ensure that the results written by one thread are visible to the other.synchronizedAnd
volatileStructureHappen-beforeLink,Thread.start()And
Thread.join()Method FormationHappen-beforeLink. Especially:
- Every operation in the threadHappen-beforeEach operation in the thread that will be passed in program order later.
- An unlock Monitor (
synchronizedBlocking or method Exit)Happen-beforeEach subsequent lock of the same Monitor (synchronizedBlocking or method access ). And because
Happen-beforeLinks can be passed, so all operations on the previous thread are unlocked.Happen-beforeLock all subsequent operations of any thread of the monitor.
- Write
volatileFieldHappen-beforeEach subsequent read of the same field.volatileThe reading and writing of fields have similar memory consistency effects with the entry and exit of the monitor,NoMutex lock is required.
- Call on Thread
start Happen-beforeAny thread in the started thread.
- All operations in the threadHappen-beforeFrom this thread
joinAny other threads returned successfully.
java.util.concurrentAnd its sub-packages extend these guarantees for higher-level synchronization. Especially:
- Operations before putting an object into any concurrent collection in a threadHappen-beforeSubsequent operations to access or remove this element from collections in another thread.
- Thread direction
ExecutorSubmitRunnablePrevious operationsHappen-beforeThe execution starts. The same applies
ExecutorServiceSubmitCallables.
- Asynchronous computing
FutureIndicates the action taken.Happen-beforeAnother threadFuture.get()Subsequent operations for obtaining results.
- "Release" synchronous storage method (such
Lock.unlock,Semaphore.releaseAnd
CountDownLatch.countDown) Previous operationsHappen-beforeIn the other line, the same synchronization storage object is successfully "obtained" (for example
Lock.lock,Semaphore.acquire,Condition.awaitAnd
CountDownLatch.await.
- For
ExchangerEach Thread Pair of the object is successfully exchanged.exchange()Previous operations
Happen-beforeCorrespondsexchange()Subsequent operations.
- Call
CyclicBarrier.awaitPrevious operationsHappen-beforeOperations performed by barrier operations and Barrier operations
Happen-beforeCorrespondsawaitSubsequent operations are returned successfully.
Start with the following versions:
1.5
| Overview |
Software Package |
Class |
Use |
Tree |
Expired |
Index |
Help |
|
Javatm Platform Standard ed. 6 |
| Previous Software PackageNext package |