1 package cn.hutool.core; 2 3 import java.util.ArrayList; 4 import java.util.LinkedList; 5 import java.util.List; 6 import java.util.concurrent.ExecutionException; 7 import java.util.concurrent.ExecutorService; 8 import java.util.concurrent.Executors; 9 import java.util.concurrent.Future; 10 11 public class TestDemo { 12 public static void main(String[] args) { 13 Runnable runnable1 = new Runnable() { 14 @Override 15 public void run() { 16 System.out.println("1111````1111111"); 17 try { 18 Thread.sleep(5000); 19 } catch (InterruptedException e) { 20 e.printStackTrace(); 21 } 22 System.out.println("1111111111111111111111"); 23 } 24 }; 25 Runnable runnable2 = new Runnable() { 26 @Override 27 public void run() { 28 System.out.println("2222````222222"); 29 try { 30 Thread.sleep(3000); 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 } 34 System.out.println("2222222222222222222222"); 35 } 36 }; 37 Runnable runnable3 = new Runnable() { 38 @Override 39 public void run() { 40 System.out.println("33333````333333"); 41 try { 42 Thread.sleep(10000); 43 } catch (InterruptedException e) { 44 e.printStackTrace(); 45 } 46 System.out.println("3333333333333333333333"); 47 } 48 }; 49 Runnable runnable4 = new Runnable() { 50 @Override 51 public void run() { 52 System.out.println("444444````444444"); 53 try { 54 Thread.sleep(5); 55 } catch (InterruptedException e) { 56 e.printStackTrace(); 57 } 58 System.out.println("444444444444444444444"); 59 } 60 }; 61 Runnable runnable5 = new Runnable() { 62 @Override 63 public void run() { 64 System.out.println("5555555````5555555555"); 65 try { 66 Thread.sleep(3); 67 } catch (InterruptedException e) { 68 e.printStackTrace(); 69 } 70 System.out.println("5555555555555555555555"); 71 } 72 }; 73 List<Future> results = new ArrayList<>(); 74 ExecutorService es = Executors.newCachedThreadPool(); 75 results.add(es.submit(runnable1)); 76 results.add(es.submit(runnable2)); 77 results.add(es.submit(runnable3)); 78 for (Future result : results) { 79 try { 80 result.get(); 81 } catch (InterruptedException e) { 82 e.printStackTrace(); 83 } catch (ExecutionException e) { 84 e.printStackTrace(); 85 } 86 } 87 Thread thread4 = new Thread(runnable4); 88 Thread thread5 = new Thread(runnable5); 89 thread4.start(); 90 thread5.start(); 91 es.shutdown(); 92 } 93 }