JDK5 Thread and contract
1) Atomicinteger
To update an int value using atomic methods
2) Create thread pool
A) Create a fixed-size thread pool
Executorservice ThreadPool = Executors.newfixedthreadpool (10);//Create a thread pool with 10 threads
b) Create a cache thread pool
Executors.newcachedthreadpool ();
Create a thread pool that can create new threads as needed to automatically adjust the number of threads
c) Creating a single thread pool
Executors.newsinglethreadexecutor ();
There is only one thread inside the thread pool, and if you die, you can create one automatically, guaranteeing that there will always be a
d) Create a thread pool that can schedule commands to run after a given delay or perform periodic
Scheduledexecutorservice ThreadPool = Executors.newscheduledthreadpool (3);
Threadpool.schedule (Runnable command,long delay,timeunit unit)
Create and execute a one-time operation enabled after a given delay
Threadpool.scheduleatfixedrate (Runnable command,long initialdelay,long period,timeunit unit)
Creates and executes a recurring action that is first enabled after a given initial delay, with a given period for subsequent operations.
That is, the execution begins after InitialDelay, then executes after Initialdelay+period, then executes after InitialDelay + 2 * period, and so on
Either way, it is actually implemented by the Threadpoolexecutor class.
3) Callable&future
A) The result type obtained by the future and the result type returned by the callable must be the same, which is achieved by generics.
b) callable to be submitted using Executorsevice's Submit method, the returned future object can cancel the task.
c) Completionservice is used to submit a set of callable tasks whose take method returns the corresponding future object for a completed callable task.
4) Lock&condition
Lock---> Synchronized
Lock lock = new Reentrantlock ();
Readwritelock Rwlock = new Reentrantreadwritelock ();
Private Condition Condition = Lock.newcondition ();
Condition.await (); ---> Wait ()
Condition.signal (); ---> Notify ()
5) Semaphore
Final Semaphore Semaphore = new Semaphore (3);
6) Cyclicbarrier
7) Countdownlatch
8) Exchanger
9) Arrayblockingqueue (blocking queue)
) Concurrenthashmap
Copyonwritearraylist
Copyonwritearrayset
This article is from the "diligence" blog, make sure to keep this source http://ybchina.blog.51cto.com/8888456/1842645
JDK5 thread and contract using collation