zoukankan      html  css  js  c++  java
  • 六、线程池

    一、什么是线程池

    创建和销毁对象是非常耗费时间的

    创建对象:需要分配内存等资源

    销毁对象:虽然不需要程序员操心,但是垃圾回收器会在后台一直跟踪并销毁

         对于经常创建和销毁、使用量特别大的资源,比如并发情况下的线程,对性能影响很大。

    思路:创建好多个线程,放入线程池中,使用时直接获取引用,不使用时放回池中。可以避免频繁创建销毁、实现重复利用

    技术案例:线程池、数据库连接池 JDK1.5 起,提供了内置线程池

    二、 线程池的好处

    1) 提高响应速度(减少了创建新线程的时间)

    2) 降低资源消耗(重复利用线程池中线程,不需要每次都创建)

    3) 提高线程的可管理性:避免线程无限制创建、从而销毁系统资源,降低系统稳定性,甚至内存溢出或者 CPU 耗尽

    三、线程池的应用场合

    1) 需要大量线程,并且完成任务的时间短

    2) 对性能要求苛刻

    3) 接受突发性的大量请求

    四、 使用线程池执行大量的 Runnable 命令

     1 public class pool1 {
     2     public static void main(String[] args) {
     3         //如何创建一个线程池
     4         //(1)创建一个线程池,线程池中只有一个线程对象
     5         //ExecutorService pool01 = Executors.newSingleThreadExecutor();
     6         //(2)创建一个线程池,线程池中有线程的数量固定
     7         ExecutorService pool01 = Executors.newFixedThreadPool(10);
     8         //(3)创建一个线程池,线程池中的线程的数量可以动态的改变
     9         //ExecutorService poolp1 = Executors.newCachedThreadPool();
    10         for (int i = 0; i < 20; i++) {
    11             final int n = i;
    12             Runnable command = new Runnable() {
    13                 @Override
    14                 public void run() {
    15                     System.out.println("开始执行:"+n);
    16                     try {
    17                         Thread.sleep(2000);
    18                     } catch (InterruptedException e) {
    19                         // TODO Auto-generated catch block
    20                         e.printStackTrace();
    21                     }
    22                     System.out.println("执行结束:"+n);
    23                 }
    24             };
    25             //任务结束
    26             //将任务交给线程池中的线程去执行
    27             pool01.execute(command);
    28         }
    29         //关闭线程池
    30         pool01.shutdown();
    31     }
    32 }

    五、 使用线程池执行大量的 Callable 任务

     1 public class pool2 {
     2     public static void main(String[] args) throws ExecutionException, InterruptedException {
     3         // 如何创建一个线程池
     4         // (1)创建一个线程池,线程池中只有一个线程对象
     5         //ExecutorService pool1=Executors.newSingleThreadExecutor();
     6         // (2)创建一个线程池,线程池中有线程的数量固定
     7         ExecutorService pool1= Executors.newFixedThreadPool(10);
     8         // (3)创建一个线程池,线程池中的线程的数量可以动态的改变
     9         //ExecutorService pool1 = Executors.newCachedThreadPool();
    10         //创建一个集合
    11         List<Future> list = new ArrayList<>();
    12         /**使用线程池执行大量的Callable任务*/
    13         for (int i = 0; i < 20; i++) {
    14             Callable<Integer> task = new Callable<Integer>() {
    15                 @Override
    16                 public Integer call() throws Exception {
    17                     Thread.sleep(2000);
    18                     return (int)(Math.random()*10)+1;
    19                 }
    20             };
    21             //任务结束
    22             //将任务交能线程池
    23             Future f = pool1.submit(task);
    24             list.add(f);
    25             //System.out.println(f.get());
    26         }
    27         System.out.println("ok?");
    28         //遍历集合
    29         for (Future future : list) {
    30             System.out.println(future.get());
    31         }
    32         System.out.println("OK!");
    33         //关闭线程池
    34         pool1.shutdown();
    35     }
    36 }
  • 相关阅读:
    [macOS] git忽略所有的.DS_Store文件
    [macOS] finder变慢提速
    [React Native] change port when running react native
    转载: 我如何使用 Django + Vue.js 快速构建项目
    MySQL Connector/NET 使用小结(踩坑之路)
    C# 控制台程序(Console Application )启动后隐藏
    解决 pycharm can not save setting
    ubuntu 16.04 LTS 安装 teamviewer 13

    Python 编程规范梳理
  • 原文地址:https://www.cnblogs.com/qiaoxin11/p/12721134.html
Copyright © 2011-2022 走看看