Java 多线程/线程池的使用

╰+哭是因爲堅強的太久メ 2022-10-16 06:10 386阅读 0赞

1)下面的代码返回什么结果:

  1. public static void main(String[] args) {
  2. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
  3. 50,
  4. 5,
  5. TimeUnit.SECONDS,
  6. new LinkedBlockingQueue<>(2),
  7. new ThreadPoolExecutor.DiscardPolicy());
  8. for(int i=0;i<10;i++){
  9. final int task = i;
  10. threadPoolExecutor.execute(() ->
  11. {
  12. System.out.println("thread is : "+ task);
  13. try {
  14. Thread.sleep(Long.MAX_VALUE);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. });
  19. }
  20. }

核心线程是3个,

最大线程数是50个,

核心线程满了后队列是能放2个,

任务是10个

实际能执行几个?

能执行8个 :第1到第3个核心线程执行;第4个,和第5个放队列中了,总是取不出来,不执行;第6到第10个新建队列执行,因为没有超过最大的限制50个。

  1. thread is : 1
  2. thread is : 0
  3. thread is : 2
  4. thread is : 5
  5. thread is : 6
  6. thread is : 7
  7. thread is : 8
  8. thread is : 9

2)下面的代码返回什么结果:

  1. public static void main(String[] args) {
  2. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
  3. 5,
  4. 5,
  5. TimeUnit.SECONDS,
  6. new LinkedBlockingQueue<>(2),
  7. new ThreadPoolExecutor.DiscardPolicy());
  8. for(int i=0;i<10;i++){
  9. final int task = i;
  10. threadPoolExecutor.execute(() ->
  11. {
  12. System.out.println("thread is : "+ task);
  13. try {
  14. Thread.sleep(Long.MAX_VALUE);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. });
  19. }
  20. }

核心线程是3个,

最大线程数是5个,

核心线程满了后队列是能放2个,

任务是10个

实际能执行几个?

能执行5个 :第1到第3个核心线程执行;第4个,和第5个放队列中了,总是取不出来,不执行;第6到第7个新建队列执行,此时会达到最大限制5个;第8个到第10个,或根据策略丢弃。

  1. thread is : 1
  2. thread is : 0
  3. thread is : 2
  4. thread is : 5
  5. thread is : 6

3)下面的代码返回什么结果:

  1. public static void main(String[] args) {
  2. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
  3. 5,
  4. 5,
  5. TimeUnit.SECONDS,
  6. new LinkedBlockingQueue<>(100),
  7. new ThreadPoolExecutor.DiscardPolicy());
  8. for(int i=0;i<10;i++){
  9. final int task = i;
  10. threadPoolExecutor.execute(() ->
  11. {
  12. System.out.println("thread is : "+ task);
  13. try {
  14. Thread.sleep(Long.MAX_VALUE);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. });
  19. }
  20. }

核心线程是3个,

最大线程数是5个,

核心线程满了后队列是能放100个,

任务是10个

实际能执行几个?

能执行3个 :第1到第3个核心线程执行;第4个到第10个都是放队列了,因为队列不满,是不会去触发创建其他线程的,就一直被放在队列中了,也取不出来。

  1. thread is : 0
  2. thread is : 1
  3. thread is : 2

4)下面的代码返回什么结果:

  1. public static void main(String[] args) {
  2. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
  3. 5,
  4. 5,
  5. TimeUnit.SECONDS,
  6. new LinkedBlockingQueue<>(5),
  7. new ThreadPoolExecutor.DiscardPolicy());
  8. for(int i=0;i<10;i++){
  9. final int task = i;
  10. threadPoolExecutor.execute(() ->
  11. {
  12. System.out.println("thread is : "+ task);
  13. try {
  14. Thread.sleep(Long.MAX_VALUE);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. });
  19. }
  20. }

核心线程是3个,

最大线程数是5个,

核心线程满了后队列是能放5个,

任务是10个

实际能执行几个?

能执行5个 :第1到第3个核心线程执行;第4个到第8个放队列中了,第9个到第10个,创建线程执行。

  1. thread is : 2
  2. thread is : 8
  3. thread is : 0
  4. thread is : 1
  5. thread is : 9

参考:

Java线程池,阿里为什么不允许使用Executors?

为什么阿里发布的Java开发手册强制不允许线程池的创建使用Executors?

为什么阿里巴巴要禁用Executors创建线程池?

发表评论

表情:
评论列表 (有 0 条评论,386人围观)

还没有评论,来说两句吧...

相关阅读

    相关 线15/使用线

    使用线程池 背景:经常创建和销毁、使用量特别大的资源,比如并发情况下的线程,对性能影响很大。 思路:提前创建好多个线程,放入线程池中,使用时直接获取,使用完放回池中。可

    相关 java线-线

    线程池-基本原理 概述 : ​ 提到池,大家应该能想到的就是水池。水池就是一个容器,在该容器中存储了很多的水。那么什么是线程池呢?线程池也是可以看做成一个池子,在该池子