Java. util. concurrent package (2) -- Thread Pool

Source: Internet
Author: User

I. Overview
Java. util. concurrent has a very convenient thread pool implementation. The Executor framework provided contains the static factory methods for managing and implementing Runnable tasks. The Executors class provides various types of Executors.


Ii. Instances
Public class MyTask implements Runnable
{
Public void run ()
{
System. out. println (Thread. currentThread (). getName () + "the task is running ");
}
}

① Fixed-size thread pool newFixedThreadPool
Public class FixedThreadPoolTest
{
Public static void main (String [] args)
{
// Create a thread pool with a fixed size of 5
ExecutorService threadPool = Executors. newFixedThreadPool (5 );
// 20 tasks are simulated, and there are only five threads in the thread pool.
For (int I = 0; I <20; I ++)
{
Try
{
// Execute five tasks at a time, and other tasks are queued
Thread. sleep (20 );
ThreadPool.exe cute (new MyTask ());
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
ThreadPool. shutdown ();
}
}
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
Pool-1-thread-5 task running
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
Pool-1-thread-5 task running
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
Pool-1-thread-5 task running
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
Pool-1-thread-5 task running

② There is no limit on the size of the thread pool newCachedThreadPool
Public class CachedThreadPool
{
Public static void main (String [] args)
{
// No limit on the thread pool size
// It will be used again when the previously created thread can be used
ExecutorService threadPool = Executors. newCachedThreadPool ();
For (int I = 0; I <20; I ++)
{
ThreadPool.exe cute (new MyTask ());
}
ThreadPool. shutdown ();
}
}
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
Pool-1-thread-4 task running
The pool-1-thread-3 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-1 task is running.
Pool-1-thread-6 task running
Pool-1-thread-4 task running
The pool-1-thread-1 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
The pool-1-thread-1 task is running.
Pool-1-thread-5 task running
The pool-1-thread-7 task is running.
The pool-1-thread-3 task is running.
The pool-1-thread-2 task is running.
Pool-1-thread-8 task running
Pool-1-thread-6 task running


③ NewSingleThreadExecutor
Public class SingleThreadPool
{
Public static void main (String [] args)
{
// Single thread pool
ExecutorService threadPool = Executors. newSingleThreadExecutor ();
For (int I = 0; I <20; I ++)
{
ThreadPool.exe cute (new MyTask ());
}
ThreadPool. shutdown ();
}
} The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.

④ NewScheduledThreadPool
Public class ScheduledPool
{
Public static void main (String [] args)
{
// Scheduled task Thread Pool
ScheduledExecutorService threadPool = Executors. newScheduledThreadPool (3 );
// Run the task three seconds later
ThreadPool. schedule (new MyTask (), 3, TimeUnit. SECONDS );
// Run in five seconds, every two seconds
ThreadPool. scheduleAtFixedRate (new MyTask (), 5, 2, TimeUnit. SECONDS );
}
}

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.