JAVA线程池

  1. 线程也是一种资源,多线程场景下频繁的创建和销毁线程均会产生性能损耗,过多不合理线程的创建也会占用大量系统资源、甚至造成系统宕机。在这个背景下“线程池”应运而生
  2. 线程池创建通过Executors产生ExecutorServices实现
ExecutorService threadPool = Executor.new******; // 创建线程池,可以创建不同类型线程池
threadPool.submit(Runnable); // 线程池申请线程,执行Runnable中的run方法
threadPool.shotDown();// 关闭线程池,释放占用资源
  1. 可以创建固定大小的线程池newFixedThreadPool,当申请线程超过最大线程数是,则进入等待队列(LinkedBlockingQueue,先进先执行),直到有任务执行完成
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Rambo(浩祥)
 * @create 2017-03-09
 **/
public class Practice {
    static int j = 0;
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        for (int i=0; i<5; i++){
            j = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ":" + j);
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        executorService.shutdown();
    }
}
image.png
  1. 可以创建缓存线程池newCachedThreadPool,申请线程如果当前线程池没有空闲线程,则创建,任务执行完毕会放入线程池维护,如果有空闲线程,则直接使用。缓存的线程超过一定时间会被销毁回收(注意事项:启动的线程数如果超过整型最大值后会抛出RejectedExecutionException异常,启动后的线程存活时间为一分钟)
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Rambo(浩祥)
 * @create 2017-03-09
 **/
public class Practice {
    static int j = 0;
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i=0; i<5; i++){
            j = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ":" + j);

                }
            });
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        executorService.shutdown();
    }
}
image.png
  1. 创建一个可定时执行的线程池(类似Timer.schedual)newScheduledThreadPool,该线程池内的线程没有执行序列,随机捞取空闲线程,比如2个线程但有可能一直用的是一个线程
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Rambo(浩祥)
 * @create 2017-03-09
 **/
public class Practice {
    static int j = 0;
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newScheduledThreadPool(2);
        ScheduledExecutorService executorService1 = Executors.newScheduledThreadPool(2);
        for (int i=0; i<5; i++){
            j = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ":" + j);

                }
            });
        }
        executorService1.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "scheduleWithFixedDelay");
            }
        }, 1, 1, TimeUnit.SECONDS);

        executorService1.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "scheduleAtFixedRate");
            }
        }, 1, 1, TimeUnit.SECONDS);

        executorService.shutdown();
    }
}
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言:线程是稀缺资源,如果被无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,合理的使用线程池对线程进行统一...
    SDY_0656阅读 765评论 0 1
  • 本篇文章讲述Java中的线程池问题,同样适用于Android中的线程池使用。本篇文章参考:Java线程池分析,Ja...
    Android进阶与总结阅读 838评论 0 5
  • 博客链接:http://www.ideabuffer.cn/2017/04/04/深入理解Java线程池:Thre...
    闪电是只猫阅读 15,939评论 15 133
  • 本篇文章讲述Java中的线程池问题,同样适用于Android中的线程池使用。本篇文章参考:Java线程池分析,Ja...
    Ruheng阅读 7,197评论 1 64
  • 昨天无意中看到轻松筹里的一条新闻,江歌和刘鑫的事件。 整个事件是这样的,一年前,日本留学生江歌接到闺蜜刘鑫的救助电...
    河仙姑阅读 379评论 1 0