Java underlying Technology series article-thread pool framework

Source: Internet
Author: User

One, thread pool structure diagram

Ii. examples

Defining thread Interfaces

public class MyThread extends Thread {
@Override
Publicvoid Run () {
System.out.println (Thread.CurrentThread (). GetName () + "executing");
}
}

1:newsinglethreadexecutor

ExecutorService pool = Executors. newSingleThreadExecutor();

Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
//关闭线程池
pool.shutdown();

Input Result:
Pool-1-thread-1 is executing
Pool-1-thread-1 is executing
Pool-1-thread-1 is executing

2:newfixedthreadpool

ExecutorService pool = Executors.newFixedThreadPool(3);
Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
Thread t4 = new MyThread();
Thread t5 = new MyThread();
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
pool.shutdown();

Input Result:
Pool-1-thread-1 is executing
Pool-1-thread-2 is executing
Pool-1-thread-1 is executing
Pool-1-thread-2 is executing

3:newcachedthreadpool

ExecutorService pool = Executors.newCachedThreadPool();
Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
Thread t4 = new MyThread();
Thread t5 = new MyThread();
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//关闭线程池
pool.shutdown();

Input Result:
Pool-1-thread-2 is executing
Pool-1-thread-4 is executing
Pool-1-thread-3 is executing
Pool-1-thread-1 is executing
Pool-1-thread-5 is executing

4:scheduledthreadpoolexecutor

ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
pool.scheduleAtFixedRate(new Runnable() {//每隔一段时间就触发异常
@Override
public void run() {
//throw new RuntimeException();
System.out.println("================");
}
}, 1000, 2000, TimeUnit.MILLISECONDS);
pool.scheduleAtFixedRate(new Runnable() {//每隔一段时间打印系统时间,证明两者是互不影响的
@Override
public void run() {
System.out.println("+++++++++++++++++");
}
}, 1000, 2000, TimeUnit.MILLISECONDS);

Input Result:

================
+++++++++++++++++
+++++++++++++++++
+++++++++++++++++

Third, thread pool core parameters
Corepoolsize: Number of threads in the pool core
Maximumpoolsize: The maximum number of threads allowed in the pool.
KeepAliveTime: When the number of threads is greater than the core, this is the maximum time to wait for a new task before terminating the extra idle thread.
The time unit of the Unit:keepalivetime parameter.
WorkQueue: The queue used to hold the task before execution. This queue keeps only the runnable tasks that are submitted by the Execute method.
Threadfactory: The factory that is used when the execution program creates a new thread.
Handler: The handler that is used when execution is blocked because it exceeds the thread range and queue capacity.
The underlying implementation of the Threadpoolexecutor:executors class.

3.1 Task queuing mechanism
Synchonousqueue: Synchronization queues, where queues are submitted directly to thread execution without maintaining them, the thread pool is usually unbounded
Linkedblockingqueue: unbounded pairs of columns, when the maximum number of thread pool threads is reached, new tasks are queued for execution and may cause the queue to swell indefinitely
Arrayblockingqueue: Bounded queues that help prevent resource exhaustion and, once capped, may cause new tasks to be lost
Attention:
Newsinglethreadexecutor, Newfixedthreadpool is using Linkedblockingqueue.
Newcachedthreadpool is using Synchonousqueue.
Newscheduledthreadpool is using Delayedworkqueue.

3.2 Thread Execution Process

3.3 Thread Size OK:
CPU-intensive: Minimum number of threads, best thread Count ncpu+1
IO-intensive: Multi-thread, 2NCPU
Hybrid: Depending on the situation, it can be split into IO-intensive and cou dense

Java underlying Technology series article-thread pool framework

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.