线程池三个使用方式

线程池三个使用方式

大厂面试题:

1、请你谈谈对volatile的理解?

2、CAS你知道吗?

3、原子类AtomicInteger的ABA问题谈谈?原子更新引用知道吗?

4、我们都知道ArrayList是线程不安全的,请编码写一个不安全的案例并给出解决方案?

5、公平锁/非公平锁/可重入锁/递归锁/自旋锁谈谈你的理解?请手写一个自旋锁。

6、CountDownLatch、CyclicBarrier、Semaphore使用过吗?

7、阻塞队列知道吗?

8、线程池用过吗?ThreadPoolExecutor谈谈你的理解?

9、线程池用过吗?生产上你是如何设置合理参数?

10、死锁编码及定位分析?

 

线程池的三个使用方式

(1)线程池第一个使用方式:Executors.newFixedThreadPool()

package com.wwl.juc;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

/**

 * 线程池三种使用方式

 * 1、Executors.newFixedThreadPool(5) 适合长期任务,性能好

 * 2、Executors.newSingleThreadExecutor() 一个任务一个任务执行,保证顺序性

 * 3、Executors.newCachedThreadPool() 适合短期异步任务或者负载很轻的服务

 */

public class ThreadPoolDemoOne {

    public static void main(String[] args) {

        // 一个线程池创建5个固定线程

        ExecutorService threadPool = Executors.newFixedThreadPool(5);

 

        // 线程池第一种使用方式:1、Executors.newFixedThreadPool(5)

        try {

            for (int i = 0; i < 10; i++) {

                threadPool.execute(() -> {

                    System.out.println(Thread.currentThread().getName() + "\t 办理业务");

                });

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

程序执行结果如下:最多只有5个线程同时执行任务

(2)线程池第二个使用方式:Executors.newSingleThreadExecutor()

package com.wwl.juc;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

/**

 * 线程池三种使用方式

 * 1、Executors.newFixedThreadPool(5) 适合长期任务,性能好

 * 2、Executors.newSingleThreadExecutor() 一个任务一个任务执行,保证顺序性

 * 3、Executors.newCachedThreadPool() 适合短期异步任务或者负载很轻的服务

 

 */

public class ThreadPoolDemoTwo {

    public static void main(String[] args) {

        // 一个线程池创建1个线程

        ExecutorService threadPoolTwo = Executors.newSingleThreadExecutor();

 

        // 线程池第二种使用方式:2、Executors.newSingleThreadExecutor()

        try {

            for (int i = 0; i < 10; i++) {

                threadPoolTwo.execute(() -> {

                    System.out.println(Thread.currentThread().getName() + "\t 办理业务");

                });

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

 

    }

}

程序执行结果如下:只有单个线程执行任务

(3)线程池第三个使用方式:Executors.newCachedThreadPool()

 

package com.wwl.juc;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

/**

 * 线程池三种使用方式

 * 1、Executors.newFixedThreadPool(5) 适合长期任务,性能好

 * 2、Executors.newSingleThreadExecutor() 一个任务一个任务执行,保证顺序性

 * 3、Executors.newCachedThreadPool() 适合短期异步任务或者负载很轻的服务

 */

public class ThreadPoolDemoThree {

    public static void main(String[] args) throws InterruptedException {

        // 一个线程池创建N个线程

        ExecutorService threadPoolThree = Executors.newCachedThreadPool();

 

        // 线程池第三种使用方式:3、Executors.newCachedThreadPool()

        for (int i = 0; i < 10; i++) {

            threadPoolThree.execute(() -> {

                System.out.println(Thread.currentThread().getName() + "\t 办理业务");

            });

                     // 模拟长期任务,让线程执行时间变长

//            Thread.sleep(200);

        }

 

    }

}

程序执行结果如下:Executors.newCachedThreadPool();适合执行短期任务,一个线程池可以创建N个线程,但是模拟长期任务,就只有一个线程执行。

模拟短期任务执行:

模拟长期任务执行:Thread.sleep(200);

猜你喜欢

转载自blog.csdn.net/longgeqiaojie304/article/details/93371812