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();
    }
}
JAVA线程池_第1张图片
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();
    }
}
JAVA线程池_第2张图片
image.png

你可能感兴趣的:(JAVA线程池)