Basic applications of the Java thread pool and java Thread Pool

Source: Internet
Author: User

Basic applications of the Java thread pool and java Thread Pool

Understanding: basic usage of atomic operation

After Java 5, it added a lot of multi-threaded content and became a concurrent thread library.

In the java. util. concurrent package and the sub-package's API help documentation, there is content related to the concurrent Library

In the java. util. concurrent. atomic package, there are operations on basic data, basic data in the array, and basic data in the class.

The AtomicIneger class can solve the problem of multi-threaded integer access.
AtomicBoolean .....
AtomicArray... operate the basic data of the array
AtomicIntegerFieldUodater <T>: basic data in the operation class.

Basic Applications of concurrent libraries:

Deep understanding about thread pools
In the past, I only knew about the thread pool.

Method 1:

Executors tool class, which creates a thread pool and a 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 five threads ExecutorService threadPool = Executors. newFixedThreadPool (5); for (int j = 1; j <= 10; j ++) {final int task = jsf-threadpool.exe cute (new Runnable () {public void run () {for (int I = 1; I <= 10; I ++) {try {Thread. sleep (30);} catch (Exception e) {e. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + "is running" + I + "for task is" + task) ;}}); // Add a task to the pool} System. out. println ("10 task is over ");}}

You will find that only five tasks are being executed each time, and the task is executed for 10 times. Then, you need to replace the five tasks.

Therefore, there are n threads in the pool and m tasks are added to the pool. The maximum number of tasks that can be executed is n, and the remaining unexecuted tasks are waiting in queue. In addition, don't end, because there are threads in the pool, although there is nothing to do, but still waiting ThreadPool. shutdownNow ()/shutdown (). When it is finished, even if there are still tasks that will be terminated, this method is used to kill all threads in the pool.


Second:

In short, the number of threads in the thread pool is unknown. When there are three tasks, there are three threads in the pool. Each thread takes a task and reduces the number of threads.
The redundant threads in the pool will be recycled. In short, the threads in the pool are dynamic.

ExecutorService threadPool = Executors.newCachedThreadPool();


Third:

There is only one thread pool. In fact, it cannot be called a thread pool. But there is a benefit of this thread pool: When the thread in the pool dies, a new thread will be automatically created. In short,
It will ensure that there is a thread in the pool, which can solve the problem. How to restart the thread after it dies

ExecutorService threadPool = Executors.newSingleThreadExecutor();

Start timer with Thread Pool

Call the schedule method of ScheduledExectorService. The returned scheduleFuture object can cancel the task.

Supports the interval repetition task Timing mode, does not directly support absolute Timing mode, needs to be converted to relative time mode


Executors. newScheduledThreadPool (3). schedule (new Runnable () {public void run () {System. out. println ("OK! ") ;}}, 3, TimeUnit. SECONDS); // print okExecutors in 3 SECONDS. newScheduledThreadPool (3 ). scheduleAtFixedRate (new Runnable () {public void run () {System. out. println ("OK! ") ;}}, 3, 2, TimeUnit. SECONDS );


// OK after 3 seconds, and OK after 2 seconds

Note that it is not provided, such as the method at this evening, so you can use the millisecond value at this evening-the millisecond value of the current time to set


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.