Java语言学习二十五(线程池)

落日映苍穹つ 2023-07-20 13:59 126阅读 0赞

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. package com.gf.erp.thread;
  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.Future;
  5. public class ThreadPoolTest {
  6. public static void main(String[] args) throws Exception
  7. {
  8. //创建固定数量线程池
  9. ExecutorService executor = Executors.newFixedThreadPool(5);
  10. //模拟提交10个任务
  11. for (int i = 0; i < 10; i++) {
  12. executor.submit(() -> {
  13. System.out.println("线程ID: " + Thread.currentThread().getId());
  14. try {
  15. //线程池中的线程等待1
  16. Thread.sleep(1000L);
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. });
  21. }
  22. executor.shutdown();
  23. System.out.println("最后执行打印...");
  24. }
  25. }

在这里插入图片描述

  1. package com.gf.erp.thread;
  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.Future;
  5. public class ThreadPoolTest {
  6. public static void main(String[] args) throws Exception
  7. {
  8. //创建固定数量线程池
  9. ExecutorService executor = Executors.newFixedThreadPool(5);
  10. //模拟提交10个任务
  11. for (int i = 0; i < 10; i++) {
  12. Future f = executor.submit(() -> {
  13. System.out.println("线程ID: " + Thread.currentThread().getId());
  14. try {
  15. //线程池中的线程等待1
  16. Thread.sleep(1000L);
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. });
  21. f.get();
  22. }
  23. executor.shutdown();
  24. System.out.println("最后执行打印...");
  25. }
  26. }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
使用Excutors线程池工厂提供的线程池实现

  1. ```csharp
  2. package com.gf.erp.thread;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.Future;
  6. public class MyThreadPool2 {
  7. public static void bubbleSort(int[] dim)
  8. {
  9. for(int i=0;i<dim.length;i++)
  10. {
  11. for(int j=0;j<dim.length-1-i;j++)
  12. {
  13. if(dim[j]>dim[j+1])
  14. {
  15. int temp = dim[j];
  16. dim[j] = dim[j+1];
  17. dim[j+1] = temp;
  18. }
  19. }
  20. }
  21. }
  22. public static void main(String[] args) throws Exception
  23. {
  24. int[][] tasks = {
  25. { 123,23,22,34,56,3,2,6,7,1,2,4,7,8,5,12,323},
  26. { 5,6,45,34,34,23,22,224,12,567,678,999,66,99},
  27. { 12,30,43,63,68,6,9,67,7,689,99,4,3,5,8,9,0},
  28. { 1,4,6,7,8,5,5,6,8,0,33,22,34,11,21,45,550,4},
  29. { 12,13,5,6,7,8,9,0,45,66,77,4,9,7,8,7,6,99,8},
  30. { 3,5,7,9,0,10,13,7,8,0,55,44,3,68,22,44,67,8}
  31. };
  32. for(int i=0;i<tasks.length;i++)
  33. {
  34. System.out.print("排序前数组:"+i+"=");
  35. for(int j=0;j<tasks[i].length;j++)
  36. System.out.print(tasks[i][j]+",");
  37. System.out.println();
  38. }
  39. ExecutorService executor = Executors.newFixedThreadPool(5);
  40. for (int i = 0; i < tasks.length; i++) {
  41. int[] tempDim = tasks[i];
  42. Future f = executor.submit(() -> {
  43. System.out.println("thread id is: " + Thread.currentThread().getId());
  44. try {
  45. Thread.sleep(1000L);
  46. bubbleSort(tempDim);
  47. } catch (InterruptedException e) {
  48. e.printStackTrace();
  49. }
  50. });
  51. System.out.println(f.get());
  52. }
  53. executor.shutdown();
  54. System.out.println("--------------here");
  55. for(int i=0;i<tasks.length;i++)
  56. {
  57. System.out.print("排序后数组:"+i+"=");
  58. for(int j=0;j<tasks[i].length;j++)
  59. System.out.print(tasks[i][j]+",");
  60. System.out.println();
  61. }
  62. }
  63. }
  64. 使用自定义线程池实现
  65. ```csharp
  66. package com.gf.erp.thread;
  67. import java.util.concurrent.ArrayBlockingQueue;
  68. import java.util.concurrent.BlockingQueue;
  69. import java.util.concurrent.ExecutorService;
  70. import java.util.concurrent.Future;
  71. import java.util.concurrent.RejectedExecutionHandler;
  72. import java.util.concurrent.ThreadFactory;
  73. import java.util.concurrent.ThreadPoolExecutor;
  74. import java.util.concurrent.TimeUnit;
  75. public class MyThreadPool {
  76. private int corePoolSize;
  77. private int maximumPoolSize;
  78. private long keepAliveTime;
  79. private TimeUnit unit;
  80. BlockingQueue<Runnable> workQueue;
  81. ThreadFactory threadFactory;
  82. RejectedExecutionHandler handler;
  83. public static void bobbleSort(int[] dim)
  84. {
  85. for(int i=0;i<dim.length;i++)
  86. {
  87. for(int j=0;j<dim.length-1-i;j++)
  88. {
  89. if(dim[j]>dim[j+1])
  90. {
  91. int temp = dim[j+1];
  92. dim[j+1] = dim[j];
  93. dim[j] = temp;
  94. }
  95. }
  96. }
  97. }
  98. public MyThreadPool(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
  99. BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,
  100. RejectedExecutionHandler handler,int[][] task)
  101. {
  102. ExecutorService es = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,
  103. unit,workQueue,threadFactory,handler);
  104. try
  105. {
  106. for(int i=0;i<task.length;i++)
  107. {
  108. int[] tempDim = task[i];
  109. Future f = es.submit(()->{
  110. bobbleSort(tempDim);
  111. });
  112. System.out.println("f="+f.get());
  113. }
  114. es.shutdown();
  115. for(int i=0;i<task.length;i++)
  116. {
  117. System.out.print("Dim:"+i+"=");
  118. for(int j=0;j<task[i].length;j++)
  119. System.out.print(task[i][j]+",");
  120. System.out.println();
  121. }
  122. }
  123. catch(Exception e)
  124. {
  125. e.printStackTrace();
  126. }
  127. }
  128. public static void main(String[] args)
  129. {
  130. int corePoolSize = 4;
  131. int maximumPoolSize = 10;
  132. long keepAliveTime = 10;
  133. TimeUnit unit = TimeUnit.MINUTES;
  134. BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(20);
  135. ThreadFactory threadFactory = new MyThreadFactory();
  136. RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
  137. int[][] tasks = {
  138. {123,23,22,34,56,3,2,6,7,1,2,4,7,8,5,12,323},
  139. {5,6,45,34,34,23,22,224,12,567,678,999,66,99},
  140. {12,30,43,63,68,6,9,67,7,689,99,4,3,5,8,9,0},
  141. {1,4,6,7,8,5,5,6,8,0,33,22,34,11,21,45,550,4},
  142. {12,13,5,6,7,8,9,0,45,66,77,4,9,7,8,7,6,99,8},
  143. {3,5,7,9,0,10,13,7,8,0,55,44,3,68,22,44,67,8}
  144. };
  145. MyThreadPool mypool = new MyThreadPool(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,
  146. threadFactory,handler,tasks);
  147. for(int i=0;i<tasks.length;i++)
  148. {
  149. for(int j=0;j<tasks[i].length;j++)
  150. System.out.println(tasks[i][j]);
  151. System.out.println("------------");
  152. }
  153. }
  154. }
  155. class MyThreadFactory implements ThreadFactory
  156. {
  157. @Override
  158. public Thread newThread(Runnable r) {
  159. return new Thread(r);
  160. }
  161. }

思考题
定义一个大数组,使用线程池最高效排序。

发表评论

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

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

相关阅读

    相关 Java线

    【二】Java线程池 一、线程池的优势 1. 降低系统资源消耗,通过重用已存在的线程,降低线程创建和销毁造成的消耗; 2. 提高系统响应速度,当有任务到达时,通

    相关 线编程学习(线原理).

    一、线程池工作流程 线程池判断核心线程池里的线程是否都在执行任务。如果不是,则创建一个新的工作线程来执行任务(需要获得全局锁)。如果核心线程池里的线程都在执行任务,则进...