Executor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class _01_T_MyExecutor implements Executor { public static void main (String[] args) { new _01_T_MyExecutor().execute(() -> System.out.println("hello executor" )); } @Override public void execute (Runnable command) { command.run(); } }
ExecutorService
1 2 3 4 5 6 7 8 9 10 public class _02_T_ExecutorService {}
Callable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class _03_T_Callable implements Callable <Integer > { @Override public Integer call () throws Exception { return 1000 ; } public static void main (String[] args) { _03_T_Callable tCallable = new _03_T_Callable(); FutureTask<Integer> futureTask = new FutureTask<>(tCallable); Thread thread = new Thread(futureTask); thread.start(); try { Integer integer = futureTask.get(); System.out.println(integer); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
Future
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class _03_T_Callable implements Callable <Integer > { @Override public Integer call () throws Exception { return 1000 ; } public static void main (String[] args) { _03_T_Callable tCallable = new _03_T_Callable(); FutureTask<Integer> futureTask = new FutureTask<>(tCallable); Thread thread = new Thread(futureTask); thread.start(); try { Integer integer = futureTask.get(); System.out.println(integer); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
Executors
1 2 3 4 5 6 7 8 9 10 public class _04_T_Executors {}
Executor.newFixedThread(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class _05_T_FixedThreadPool { public static void main (String[] args) throws InterruptedException { ExecutorService service = Executors.newFixedThreadPool(5 ); for (int i = 0 ; i < 6 ; i++) { service.execute(() -> { try { TimeUnit.MILLISECONDS.sleep(500 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); }); } System.out.println(service); service.shutdown(); System.out.println(service.isTerminated()); System.out.println(service.isShutdown()); System.out.println(service); TimeUnit.SECONDS.sleep(5 ); System.out.println(service.isTerminated()); System.out.println(service.isShutdown()); System.out.println(service); } }
练习:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 public class _07_T_ParallelComputing { public static void main (String[] args) throws ExecutionException, InterruptedException { long start = System.currentTimeMillis(); List<Integer> result = getPrime(1 , 200000 ); long end = System.currentTimeMillis(); System.out.println(end - start); final int cpuCoreNum = 4 ; ExecutorService service = Executors.newFixedThreadPool(cpuCoreNum); MyTask t1 = new MyTask(1 , 80000 ); MyTask t2 = new MyTask(80001 , 130000 ); MyTask t3 = new MyTask(130001 , 170000 ); MyTask t4 = new MyTask(170001 , 200000 ); Future<List<Integer>> future1 = service.submit(t1); Future<List<Integer>> future2 = service.submit(t2); Future<List<Integer>> future3 = service.submit(t3); Future<List<Integer>> future4 = service.submit(t4); start = System.currentTimeMillis(); future1.get(); future2.get(); future3.get(); future4.get(); end = System.currentTimeMillis(); System.out.println(end - start); service.shutdown(); } static class MyTask implements Callable <List <Integer >> { int startPos, endPos; public MyTask (int startPos, int endPos) { this .startPos = startPos; this .endPos = endPos; } @Override public List<Integer> call () throws Exception { List<Integer> prime = getPrime(startPos, endPos); return prime; } } private static boolean isPrime (int num) { for (int i = 2 ; i < num / 2 ; i++) { if (num % i == 0 ) return false ; } return true ; } private static List<Integer> getPrime (int start, int end) { List<Integer> results = new ArrayList<>(); for (int i = start; i < end; i++) { if (isPrime(i)) results.add(i); } return results; } }
Executor.newCachedTreadPool()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class _08_T_CachedThreadPool { public static void main (String[] args) throws InterruptedException { ExecutorService service = Executors.newCachedThreadPool(); System.out.println(service); for (int i = 0 ; i < 2 ; i++) { service.execute(() -> { try { TimeUnit.MILLISECONDS.sleep(500 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); }); } System.out.println(service); TimeUnit.SECONDS.sleep(70 ); System.out.println(service); } }
Executors.newSingleThreadExecutor()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class _09_T_SingleThreadPool { public static void main (String[] args) { ExecutorService service = Executors.newSingleThreadExecutor(); for (int i = 0 ; i < 5 ; i++) { final int j = i; service.execute(() -> { System.out.println(j + " " + Thread.currentThread().getName()); }); } service.shutdown(); } }
Executors.newScheduledThreadPool(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class _10_T_ScheduledThreadPool { public static void main (String[] args) throws InterruptedException { ScheduledExecutorService service = Executors.newScheduledThreadPool(4 ); service.scheduleAtFixedRate(() -> { try { TimeUnit.MILLISECONDS.sleep(1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); }, 0 , 500 , TimeUnit.MILLISECONDS); TimeUnit.SECONDS.sleep(20 ); service.shutdown(); } }
Executors.newWorkStealingPool()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class _11_T_WorkStealingPool { public static void main (String[] args) throws IOException { ExecutorService service = Executors.newWorkStealingPool(); System.out.println(Runtime.getRuntime().availableProcessors()); service.execute(new R(1000 )); service.execute(new R(2000 )); service.execute(new R(2000 )); service.execute(new R(2000 )); service.execute(new R(2000 )); System.in.read(); } static class R implements Runnable { int time; public R (int time) { this .time = time; } @Override public void run () { try { TimeUnit.MILLISECONDS.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(time + " " + Thread.currentThread().getName()); } } }
ForkJoinPool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 public class _12_T_ForkJoinPool { static int [] nums = new int [1000000 ]; static final int MAX_NUM = 50000 ; static Random r = new Random(); static { for (int i = 0 ; i < nums.length; i++) { nums[i] = r.nextInt(100 ); } System.out.println(Arrays.stream(nums).sum()); } static class AddAction extends RecursiveAction { int start, end; public AddAction (int start, int end) { this .start = start; this .end = end; } @Override protected void compute () { if (end - start <= MAX_NUM) { long sum = 0L ; for (int i = start; i < end; i++) { sum += nums[i]; } System.out.println("from:" + start + " to:" + end + " = " + sum); } else { int middle = start + (end - start) / 2 ; AddAction subAct1 = new AddAction(start, middle); AddAction subAct2 = new AddAction(middle, end); subAct1.fork(); subAct2.fork(); } } } static class AddTask extends RecursiveTask <Long > { int start, end; public AddTask (int start, int end) { this .start = start; this .end = end; } @Override protected Long compute () { if (end - start <= MAX_NUM) { long sum = 0L ; for (int i = start; i < end; i++) { sum += nums[i]; } System.out.println("from:" + start + " to:" + end + " = " + sum); return sum; } else { int middle = start + (end - start) / 2 ; AddTask subTask1 = new AddTask(start, middle); AddTask subTask2 = new AddTask(middle, end); subTask1.fork(); subTask2.fork(); return subTask1.join() + subTask2.join(); } } } public static void main (String[] args) throws IOException, ExecutionException, InterruptedException { ForkJoinPool fjp = new ForkJoinPool(); AddAction addAction = new AddAction(0 , nums.length); fjp.execute(addAction); System.in.read(); AddTask addTask = new AddTask(0 , nums.length); Future<Long> submit = fjp.submit(addTask); Long result = submit.get(); System.out.println(result); } }
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !