Basic application of the Java thread pool

Source: Internet
Author: User


Understanding: Basic usage of atomic operations classes

After Java5, the content of many threads is added to become the thread concurrency library.

API Help documentation for java.util.concurrent packages and sub-packages with content for concurrent libraries

In the Java.util.concurrent.atomic package, there are operations on basic data, basic data in an array, basic data in a class, etc.

Atomicineger class, can solve the problem of multi-threaded access integer
Atomicboolean .....
Atomicarray ... Manipulating the basic data of an array
Atomicintegerfielduodater<t>, basic data in the Action class

Basic applications for concurrent libraries:

Understanding deepening of the thread pool
I used to know this thing about the thread pool, but I didn't know it.

The first way:

Executors tool class, creating a thread pool, fixed thread pool

Import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class ThreadAtomic {public static void Main (string[] args) {//thread pool of 5 threads executorservice ThreadPool = Executors.newfixedthreadpool (5); for (int j = 1;j& lt;=10;j++) {Final int task = J;threadpool.execute (New Runnable () {public void run () {for (int i = 1;i<=10;i++) {try {Threa D.sleep (30);} catch (Exception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "is running" +i+ "for task is" + Task);}}); /Add task}system.out.println to the pool ("Ten task Is Over");}}

Will find that only 5 tasks are executed, executed to 10 times, and then replaced by 5 tasks each time.

So, there are n threads in the pool, a m task is added to the pool, the maximum number of tasks that can be performed is n, and the remaining tasks that are not executed are queued. And, not the end, because the pool is also wired, although nothing dry, but still waitingThreadpool.shutdownnow ()/shutdown (), end, even if there is a task will be ended, this method is to kill all the threads in the pool.


The second type:

The cached thread pool, simply the number of threads in the thread pools is not deterministic, when there are 3 tasks, the pool thread is 3, each thread takes a task to do when the task is reduced
, the extra threads in the pool are recycled, in short, the threads in the pool are dynamic

Executorservice ThreadPool = Executors.newcachedthreadpool ();


The third type:

There is only one thread pool, which is not actually called the thread pool, but the thread pool has the advantage that the threads in the pool are dead and a new thread is created automatically, in short,
Will ensure that there is a thread in the pool, which can be resolved, how to reboot after the threads have died

Executorservice ThreadPool = Executors.newsinglethreadexecutor ();

Start the timer with the thread pool

Call the Scheduledexectorservice schedule method, and the returned Schedulefuture object can cancel the task

Support interval Repetitive task timing mode, do not directly support absolute timing mode, need to convert to relative time mode


Executors.newscheduledthreadpool (3). Schedule (new Runnable () {public void run () {System.out.println ("ok!");}}, 3, Timeunit.seconds);//3 seconds after printing Okexecutors.newscheduledthreadpool (3). Scheduleatfixedrate (New Runnable () {public void run () {System.out.println ("ok!");}, 3,2, timeunit.seconds);


3 seconds later OK, every 2 seconds after OK

Note that it is not provided, like tonight at 9 o ' Night ... method, so you can use the millisecond value of 9 o'clock this evening-the millisecond value of the current time, to set


Basic application of the Java thread pool

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.