一、线程池概述

线程池:创建并管理(回收、销毁、分配)线程,在java编程中常见的线程池主要有四个
单线程池:线程池里面只有一个线程,该线程池负责执行所有的任务,主要用于处理一些后台不紧急任务,也适用于那些必须按照某种先后顺序执行的任务
固定线程池:线程池中的线程数量是固定的,适用于性能不太好的程序做到限流的效果(秒杀、rabbitmq)
缓存线程池(可变线程池):线程池数量可以根据任务量的大小自动增加减少线程,目的在于尽快做出响应,尽可能提高吞吐量
周期线程池
**
用于测试的任务:

  1. public class MyRunnable implements Runnable{
  2. public String name;
  3. public MyRunnable(String name) {
  4. super();
  5. this.name = name;
  6. }
  7. @Override
  8. public void run() {
  9. System.out.println(Thread.currentThread().getId()+"-执行任务-"+name);
  10. }
  11. }

**

二、单线程池

  1. //单线程池
  2. public class SinglePool {
  3. public static void main(String[] args) {
  4. //1.创建线程池
  5. ExecutorService pool = Executors.newSingleThreadExecutor();
  6. //2.创建任务
  7. MyRunnable r1 = new MyRunnable("任务1");
  8. MyRunnable r2 = new MyRunnable("任务2");
  9. MyRunnable r3 = new MyRunnable("任务3");
  10. MyRunnable r4 = new MyRunnable("任务4");
  11. //3.执行任务
  12. pool.execute(r1);
  13. pool.execute(r2);
  14. pool.execute(r3);
  15. pool.execute(r4);
  16. }
  17. }

三、固定线程池

  1. //固定线程池
  2. public class FixedPool {
  3. public static void main(String[] args) {
  4. //1.创建线程池
  5. ExecutorService pool = Executors.newFixedThreadPool(5);
  6. //2.创建任务
  7. MyRunnable r1 = new MyRunnable("任务1");
  8. MyRunnable r2 = new MyRunnable("任务2");
  9. MyRunnable r3 = new MyRunnable("任务3");
  10. MyRunnable r4 = new MyRunnable("任务4");
  11. //3.执行任务
  12. pool.execute(r1);
  13. pool.execute(r2);
  14. pool.execute(r3);
  15. pool.execute(r4);
  16. }
  17. }

四、缓存线程池

//缓存线程池
public class CachePool {
    public static void main(String[] args) {
        //1.创建线程池
        ExecutorService pool = Executors.newCachedThreadPool();
        //2.创建任务
        MyRunnable r1 = new MyRunnable("任务1");
        MyRunnable r2 = new MyRunnable("任务2");
        MyRunnable r3 = new MyRunnable("任务3");
        MyRunnable r4 = new MyRunnable("任务4");
        //3.执行任务
        pool.execute(r1);
        pool.execute(r2);
        pool.execute(r3);
        pool.execute(r4);
    }
}

五、周期线程池

//周期线程池
public class ScheduledPool {
    public static void main(String[] args) {
        //参数:能够周期性执行几个任务
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);

        //执行任务
        //参数1:任务
        //参数2:第一次执行时延迟多少
        //参数3:间隔事件
        //参数4:事件单位
        pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(new Date());
            }
        }, 5, 1, TimeUnit.SECONDS);
    }
}

六、堆栈,队列

6.1 堆栈(先进后出)

压栈,进栈:push
弹栈,出栈:pop

/*
 * 堆栈
 */
public class Stack {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list); // [A,B,C]

        LinkedList<String> stack = new LinkedList<String>();
        //压栈
        stack.push("A");
        stack.push("B");
        stack.push("C");
        System.out.println(stack); // [C,B,A]

        //弹栈
        stack.pop();
        System.out.println(stack);    //[B,A]

        stack.pop();
        System.out.println(stack);    //[A]
    }    
}

6.2 队列(先进先出)

入队列:offer
出队列:poll

/*
 * 队列
 */
public class QueueTest {
    public static void main(String[] args) {
        LinkedList<String> queue = new LinkedList<String>();

        //入队列
        queue.offer("A");
        queue.offer("B");
        queue.offer("C");
        queue.offer("D");
        System.out.println(queue);    //    [A, B, C, D]

        //出队列
        queue.poll();
        System.out.println(queue);    //    [B,C,D]

    }
}