为什么要使用线程池?
第一,减少线程的创建和销毁次数,并允许每个工作线程重用以执行多项任务。
第二,根据系统的容错能力调整线程池中的工作线程数,以防止服务器因占用过多的内存而疲惫不堪。 (每个线程需要约1MB的内存,线程越多,占用的内存也越大,最终死机。
线程池的基本思想是池的映像,它打开包含许多(未死)线程的内存空间,池中的线程执行调度由池管理器处理。 通过从池中提取一个线程任务(如果有),并在运行完成时池中池线程对象,可以避免重复创建线程对象带来的性能开销,从而节省系统资源。
因此,线程池的作用主要是限制系统中的执行线程数。 根据系统环境情况,可以自动或手动设置线程数以获得最佳结果。 这是因为线程少会浪费系统资源,线程多会导致系统拥塞,效率低下。 在线程池中控制线程数,其他线程排队等待。 执行完一个任务后,从队列中取出第一个任务开始执行。 如果队列中没有等待进程,则线程池中的此资源正在等待。 如果需要执行新任务,则可以在线程池中启动正在等待的工作线程,否则将进入队列。
Java中线程池的顶部接口是Executor,但严格意义上说,Executor是运行线程的工具,而不是线程池。 真正的线程池接口是ExecutorService。 ThreadPoolExecutor是Executors类的基础实现。 让我先介绍一下Executors。
在使用线程池之前,必须知道如何创建线程池。
1 .固定大小的线程池
package com.itszt.test3;
import Java.util.concurrent.executorservice;
import Java.util.concurrent.executors;
//*
*线程池
*/
公共类测试1扩展对象{
publicstaticvoidmain (字符串[ ] args ) {
//创建可重用且线程数固定的线程池
executorservicethreadpool=executors.newfixedthreadpool (2;
创建实现Runnable接口的类,如Thread
MyThread t1=new MyThread (;
MyThread t2=new MyThread (;
MyThread t3=new MyThread (;
MyThread t4=new MyThread (;
//将线程放入池中运行
threadpool.execute(T1;
threadpool.execute(T2;
threadpool.execute(T3;
threadpool.execute(T4;
//关闭线程池
threadPool.shutdown (;
}
}
class MyThread extends Thread{
@Override
公共void run (}
系统. out.println (thread.current thread ().getName ) ) '正在运行.');
}
}
执行结果如下。
pool-1-thread-1正在运行.
pool-1-thread-2正在运行.
pool-1-thread-1正在运行.
pool-1-thread-1正在运行.
2 .单任务线程池
重复使用上述代码,将在上例中创建线程池的代码更改为:
executorservicethreadpool=executors.newsinglethreadexecutor (;
执行结果如下。
pool-1-thread-1正在运行.
pool-1-thread-1正在运行.
pool-1-thread-1正在运行.
pool-1-thread-1正在运行.
3 .大小可变的线程池
更改线程池的创建方法:
executorservicethreadpool=executors.newcachedthreadpool (
执行结果如下。
pool-1-thread-2正在运行.
pool-1-thread-1正在运行.
pool-1-thread-3正在运行.
pool-1-thread-4正在运行.
4 .延迟连接池
更改线程池的创建方法:
scheduledexecutorservicethreadpool=executors.newscheduledthreadpool (2);
MyThread t1=new MyThread (;
MyThread t2=new MyThread (;
MyThread t3=new MyThread (;
MyThread t4=new MyThread (;
//延迟执行
thread pool.schedule (t1,5,TimeUnit.MILLISECONDS );
thread pool.schedule (T2,5,TimeUnit.MILLISECONDS );
thread pool.schedule (T3,5,TimeUnit.MILLISECONDS );
thread pool.schedule (T4,5,TimeUnit.MILLISECONDS );
//关闭线程池
threadPool.shutdown (;
5 .单任务延迟连接池
更改线程池的创建方法:
scheduledexecutorservicethreadpool=executors.newsinglethreadscheduledexecutor (;
6 .自定义线程池
package com.itszt.test3;
导入Java.text.simple date format;
import java.util.Date;
import Java.util.concurrent.arrayblockingqueue;
import Java.util.concurrent.blocking queue;
import Java.util.concurrent.thread pool executor;
import Java.util.concurrent.time unit;
//*
*自定义线程池
*/
公共类测试2 {
publicstaticvoidmain (字符串[ ] args ) {
//排队等候
blockingqueuebqueue=newarrayblockingqueue (20;
//创建一个单线程执行程序,可以计划在指定的延迟时间后运行
threadpoolexecutorpool=newthreadpoolexecutor (2,3,2,TimeUnit.MILLISECONDS,bQueue );
创建实现Runnable接口的类,如Thread
MyThread t1=new MyThread (;
MyThread t2=new MyThread (;
MyThread t3=new MyThread (;
MyThread t4=new MyThread (;
//将线程放入池中运行
pool.execute(T1;
pool.execute(T2;
pool.execute(T3;
pool.execute(T4;
//关闭线程池
pool.shutdown (;
}
}
class MyThread extends Thread{
@Override
公共void run (}
system.out.println (thread.current thread (.getname ) ) '正在运行. ' System.currentTimeMillis ' );
}
}