JDK5 thread and contract using collation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.