package com.demo.mp;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadDemo {
/** * 创建线程及书写业务代码 */
static class TxTask implements Runnable {
@Override
public void run() {
System.err.println("多线程业务代码执行......");
}
}
/** 使用线程池执行多线程 */
public static void method() {
// 创建线程池
ThreadPoolExecutor executor =
new ThreadPoolExecutor(200, 500, 601, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100));
// 提交线程任务 +1000个线程
for (int i = 0; i < 1000; i++) {
executor.execute(new TxTask());
}
// 线程池关闭
executor.shutdown();
}
/**
* * 每五秒执行一次method方法创建1000个线程
*
* @param args
*/
public static void main(String[] args) {
while (true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 创建1000个线程
method();
// 告诉垃圾收集器打算进行垃圾收集,而垃圾收集器进不进行收集是不确定的
System.gc();
}
}
}
package com.demo.mp;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolDemo {
/** * 创建线程及书写业务代码 */
static class TxTask implements Runnable {
@Override
public void run() {
System.err.println(Thread.currentThread().getName() + "多线程业务代码执行......");
}
}
public static void main(String[] args) {
// 创建线程池
ThreadPoolExecutor executor =
new ThreadPoolExecutor(5, 10, 601, TimeUnit.SECONDS, new LinkedBlockingDeque<>(5));
// 提交线程任务 15 个线程
for (int i = 0; i < 15; i++) {
executor.execute(new TxTask());
}
// 线程池关闭
executor.shutdown();
}
}
ThreadPoolExecutor构造器:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueu) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
threadFactory, defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), handler);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.demo.mp.ThreadPoolDemo$TxTask@5b6f7412 rejected from java.util.concurrent.ThreadPoolExecutor@27973e9b[Running, pool size = 10, active threads = 6, queued tasks = 0, completed tasks = 27]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at com.demo.mp.ThreadPoolDemo.main(ThreadPoolDemo.java:29)
threadFactory: 线程工厂;用于创建新线程。threadFactory创建的线程也是采用new Thread()方式,threadFactory创建的线程名都具有统一的风格:pool-m-thread-n(m为线程池的编号,n为线程池内的线程编号)。
handler: (线程饱和策略):当线程池和队列都满了,再加入线程会执行对应的策略。
后面的明天再写出去办点事。。。。。。