线程池

1.线程池的概念及作用线程池,其实就是一个容纳多个线程的容器,

其中的线程可以反复使用,省去了频繁创建线程对象的操作,无需反复创建线程而消耗过多资源。

2.线程池的使用方法

public static void main(String[] args) throws InterruptedException, ExecutionException {
        //创建线程任务
        MyCallable mc = new MyCallable();
        //创建线程对象
        ExecutorService es = Executors.newFixedThreadPool(2);
        //执行线程任务
        Future <String> f = es.submit(mc);
        //获取返回值
        String str= f.get();    
        System.out.println(str);        
    }
public class MyCallable implements Callable<String> {

    public String call() throws Exception {    
        
        return "呵呵";
        
    }

注意 : 这里实现线程任务接口改用Callable<T> ,他可以返回一个指定泛型的返回值

原文地址:https://www.cnblogs.com/lxzwhite/p/10553282.html