栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

线程池的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程池的使用

1. 三大方法
  • 建立单个线程的线程池
public class juc_pooldemo {
    public static void main(String[] args) {
         ExecutorService pool = Executors.newSingleThreadExecutor();  //单个线程的线程池
        try {
            for (int i = 0; i < 10; i++) {
                pool.execute(() -> {  //lanmbda表达式创建线程,并执行线程
                    System.out.println(Thread.currentThread().getName());
                    System.out.println(Runtime.getRuntime().availableProcessors());  //获得cpu核数
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            pool.shutdown();  //关闭线程池
        }
    }
}
  • 建立固定线程数的线程池
public class juc_pooldemo {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(5);  //固定数量的线程池
        try {
            for (int i = 0; i < 10; i++) {
                pool.execute(() -> {  //lanmbda表达式创建线程,并执行线程
                    System.out.println(Thread.currentThread().getName());
                    System.out.println(Runtime.getRuntime().availableProcessors());  //获得cpu核数
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            pool.shutdown();  //关闭线程池
        }
    }
}
  • 建立可变线程数的线程池
public class juc_pooldemo {
    public static void main(String[] args) {
      ExecutorService pool = Executors.newCachedThreadPool();  //可变线程池,允许的最大数量为Integer.MAX
        try {
            for (int i = 0; i < 10; i++) {
                pool.execute(() -> {  //lanmbda表达式创建线程,并执行线程
                    System.out.println(Thread.currentThread().getName());
                    System.out.println(Runtime.getRuntime().availableProcessors());  //获得cpu核数
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            pool.shutdown();  //关闭线程池
        }
    }
}
小结
ExecutorService pool =Executors.newSingleThreadExecutor();  //单个线程的线程池 
ExecutorService pool = Executors.newFixedThreadPool(5);  //固定数量的线程池
ExecutorService pool = Executors.newCachedThreadPool();  //可变线程池,允许的最大数量为Integer.MAX
2.自定义线程池
public class juc_pooldemo {
    public static void main(String[] args) {
      ExecutorService pool = new ThreadPoolExecutor(2,7,10,
                   TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(3));//用ThreadPoolExecutor自定义线程池
        try {
            for (int i = 0; i < 10; i++) {
                pool.execute(() -> {  //lanmbda表达式创建线程,并执行线程
                    System.out.println(Thread.currentThread().getName());
                    System.out.println(Runtime.getRuntime().availableProcessors());  //获得cpu核数
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            pool.shutdown();  //关闭线程池
        }
    }
}
ThreadPoolExecutor7大参数
public ThreadPoolExecutor(int corePoolSize,  //核心线程数
                              int maximumPoolSize,  //最大线程数
                              long keepAliveTime,  //存活时间
                              TimeUnit unit,  //存活时间单位
                              BlockingQueue workQueue,  //阻塞队列
                              ThreadFactory threadFactory,  //线程工厂
                              RejectedExecutionHandler handler)  //拒绝服务策略
四种拒绝服务策略

3.线程池执行线程
ExecuteService pool = Executors.newfixedThreadPool(5);
pool.execute(new Runnable(){
//业务代码
});
//FutureTask futuretask = new FutureTask(new callable(){
//业务代码
//});  //callable接口实现多线程
//pool.execute(futuretask);
//  E temp=futuretask.get(); //获取线程返回数据
pool.shutdown();  //关闭线程池一般放在finally语句块中
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310110.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号