线程池
- 背景:经常创建和销毁、使用量特别大的资源,比如并发情况下的线程,对性能影响很大;
- 思路:提前创建好多个线程,放入线程池中,使用时直接获取,使用完放回池中。可以避免频繁创建销毁、实现重复利用。类似生活中的公共交通工具;
- 好处:
- 提高响应速度(减少了创建新线程的时间)
- 降低资源消耗(重复利用线程池中的线程,不需要每次都创建)
- 便于线程管理(…)
corePoolSize:
核心池的大小maximumPoolSize:
最大线程数keepAliveTime:
线程没有任务时保持多长时间后会终止
JDK 5.0
起提供了线程池相关API:ExecutorService和Executors
ExecutorService:
真正的线程池接口。常见的子类ThreadPoolExecutor
void execute(Runnable command)
:执行任务/命令,没有返回值,一般用来执行Runnable
<T> Future<T> submit(Callable<T> task):
执行任务,有返回值,一般又来执行Callablevoid shutdown():
关闭连接池;
Executors:
工具类、线程池的工厂类,用于创建并返回不同类型的线程池;
public class TestThreadExecutor {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(10);
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.shutdown();
}
}
class MyThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
pool-1-thread-1
pool-1-thread-4
pool-1-thread-3
pool-1-thread-2
pool-1-thread-5
public class TestCallable implements Callable<Boolean> {
private String url;
private String name;
public TestCallable(String url,String name){
this.url = url;
this.name = name;
}
@Override
public Boolean call() {
WebDownloader3 webDownloader = new WebDownloader3();
webDownloader.downloader(url,name);
System.out.println("下载的文件名为:" + name);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable t1 = new TestCallable("https://img-blog.csdnimg.cn/48a7e900dbbe409585fe118a293f74df.png","1.jpg");
TestCallable t2 = new TestCallable("https://img-blog.csdnimg.cn/48a7e900dbbe409585fe118a293f74df.png","2.jpg");
TestCallable t3 = new TestCallable("https://img-blog.csdnimg.cn/48a7e900dbbe409585fe118a293f74df.png","3.jpg");
ExecutorService ser = Executors.newFixedThreadPool(3);
Future<Boolean> result1 = ser.submit(t1);
Future<Boolean> result2 = ser.submit(t2);
Future<Boolean> result3 = ser.submit(t3);
boolean b1 = result1.get();
boolean b2 = result2.get();
boolean b3 = result3.get();
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
ser.shutdownNow();
}
}
class WebDownloader3{
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("Io异常,download方法异常!");
}
}
}
public class ThreadStyle {
public static void main(String[] args) {
new MyThread1().start();
new Thread(new MyThread2()).start();
FutureTask<Integer> future = new FutureTask<Integer>(new MyThread3());
new Thread(future).start();
try {
Integer integer = future.get();
System.out.println("方式3获取的返回值:" + integer);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class MyThread1 extends Thread{
@Override
public void run() {
System.out.println("MyThread1");
}
}
class MyThread2 implements Runnable {
@Override
public void run() {
System.out.println("MyThread2");
}
}
class MyThread3 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("MyThread3");
return 520;
}
}
MyThread1
MyThread2
MyThread3
方式3获取的返回值:520