Guest User

Untitled

a guest
Oct 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. //(1)继承Thread类
  2. //直接extend Thread,复写run()方法
  3. public class MyThread extends Thread {
  4.   public void run() {
  5.    System.out.println("MyThread.run()");
  6.   }
  7. }
  8.  
  9. //调用:
  10. MyThread myThread1 = new MyThread();
  11. MyThread myThread2 = new MyThread();
  12. myThread1.start();
  13. myThread2.start();
  14.  
  15. //(2) 实现Runnable接口
  16. //已经extends另一个类,无法直接extends Thread,实现一个Runnable接口
  17. public class MyThread extends OtherClass implements Runnable {
  18.   public void run() {
  19.    System.out.println("MyThread.run()");
  20.   }
  21. }
  22.  
  23. //调用
  24. MyThread myThread = new MyThread();
  25. Thread thread = new Thread(myThread);
  26. thread.start();
  27.  
  28.  
  29. //(3) 使用ExecutorService、Callable、Future实现有返回结果的多线程
  30. import java.util.concurrent.*;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.ArrayList;
  34.  
  35. /**
  36. * Java线程:有返回值的线程
  37. *
  38. * @author wb_qiuquan.ying
  39. */
  40. @SuppressWarnings("unchecked")
  41. public class Test {
  42. public static void main(String[] args) throws ExecutionException,
  43. InterruptedException {
  44. System.out.println("----程序开始运行----");
  45. Date date1 = new Date();
  46.  
  47. int taskSize = 5;
  48. // 创建一个线程池
  49. ExecutorService pool = Executors.newFixedThreadPool(taskSize);
  50. // 创建多个有返回值的任务
  51. List<Future> list = new ArrayList<Future>();
  52. for (int i = 0; i < taskSize; i++) {
  53. Callable c = new MyCallable(i + " ");
  54. // 执行任务并获取Future对象
  55. Future f = pool.submit(c);
  56. // System.out.println(">>>" + f.get().toString());
  57. list.add(f);
  58. }
  59. // 关闭线程池
  60. pool.shutdown();
  61.  
  62. // 获取所有并发任务的运行结果
  63. for (Future f : list) {
  64. // 从Future对象上获取任务的返回值,并输出到控制台
  65. System.out.println(">>>" + f.get().toString());
  66. }
  67.  
  68. Date date2 = new Date();
  69. System.out.println("----程序结束运行----,程序运行时间【"
  70. + (date2.getTime() - date1.getTime()) + "毫秒】");
  71. }
  72. }
  73.  
  74. class MyCallable implements Callable<Object> {
  75. private String taskNum;
  76.  
  77. MyCallable(String taskNum) {
  78. this.taskNum = taskNum;
  79. }
  80.  
  81. public Object call() throws Exception {
  82. System.out.println(">>>" + taskNum + "任务启动");
  83. Date dateTmp1 = new Date();
  84. Thread.sleep(1000);
  85. Date dateTmp2 = new Date();
  86. long time = dateTmp2.getTime() - dateTmp1.getTime();
  87. System.out.println(">>>" + taskNum + "任务终止");
  88. return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";
  89. }
  90. }
  91.  
  92. /*
  93. 代码说明:
  94. 上述代码中Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。
  95. public static ExecutorService newFixedThreadPool(int nThreads)
  96. 创建固定数目线程的线程池。
  97. public static ExecutorService newCachedThreadPool()
  98. 创建一个可缓存的线程池,调用execute 将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有 60 秒钟未被使用的线程。
  99. public static ExecutorService newSingleThreadExecutor()
  100. 创建一个单线程化的Executor。
  101. public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
  102. 创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。
  103.  
  104. ExecutoreService提供了submit()方法,传递一个Callable,或Runnable,返回Future。如果Executor后台线程池还没有完成Callable的计算,这调用返回Future对象的get()方法,会阻塞直到计算完成。
  105. */
Add Comment
Please, Sign In to add comment