|
| 1 | +package java0.conc0303; |
| 2 | + |
| 3 | +import java.util.concurrent.*; |
| 4 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 5 | +import java.util.concurrent.atomic.AtomicInteger; |
| 6 | +import java.util.concurrent.locks.Condition; |
| 7 | +import java.util.concurrent.locks.Lock; |
| 8 | +import java.util.concurrent.locks.ReentrantLock; |
| 9 | +import java.util.function.Supplier; |
| 10 | + |
| 11 | +public class HomeworkDemo1 { |
| 12 | + public static void main(String[] args) { |
| 13 | + |
| 14 | +// long start = System.currentTimeMillis(); |
| 15 | +// // 在这里创建一个线程或线程池, |
| 16 | +// // 异步执行 下面方法 |
| 17 | +// |
| 18 | +// int result = sum(); //这是得到的返回值 |
| 19 | +// |
| 20 | +// // 确保 拿到result 并输出 |
| 21 | +// System.out.println("异步计算结果为:" + result); |
| 22 | +// |
| 23 | +// System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms"); |
| 24 | +// |
| 25 | +// // 然后退出main线程 |
| 26 | + run(bySleep, "bySleep"); |
| 27 | + |
| 28 | + run(byJoin, "byJoin"); |
| 29 | + |
| 30 | + run(byCAS, "byCAS"); |
| 31 | + |
| 32 | + run(byLock, "byLock"); |
| 33 | + |
| 34 | + run(byObjectNotify, "byObjectNotify"); |
| 35 | + |
| 36 | + run(byCountDownLatch, "byCountDownLatch"); |
| 37 | + |
| 38 | + run(byFuture, "byFuture"); |
| 39 | + |
| 40 | + run(byFutureTask, "byFutureTask"); |
| 41 | + |
| 42 | + run(byCompletableFuture, "byCompletableFuture"); |
| 43 | + } |
| 44 | + |
| 45 | + public static void run(Supplier<Integer> supplier, String type) { |
| 46 | + System.out.println(type); |
| 47 | + long start = System.currentTimeMillis(); |
| 48 | + // 在这里创建一个线程或线程池, |
| 49 | + // 异步执行 下面方法 |
| 50 | + // 这是得到的返回值 |
| 51 | + int result = supplier.get(); |
| 52 | + |
| 53 | + // 确保 拿到result 并输出 |
| 54 | + System.out.print("异步计算结果为:" + result + " "); |
| 55 | + |
| 56 | + System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms"); |
| 57 | + |
| 58 | + // 然后退出main线程 |
| 59 | + } |
| 60 | + |
| 61 | + private static int sum() { |
| 62 | + return fibo(36); |
| 63 | + } |
| 64 | + |
| 65 | + private static int fibo(int a) { |
| 66 | + if (a < 2) { |
| 67 | + return 1; |
| 68 | + } |
| 69 | + return fibo(a - 1) + fibo(a - 2); |
| 70 | + } |
| 71 | + |
| 72 | + private static final Supplier<Integer> bySleep = () -> { |
| 73 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 74 | + AtomicInteger result = new AtomicInteger(); |
| 75 | + executorService.execute(() -> result.set(sum())); |
| 76 | + try { |
| 77 | + Thread.sleep(1000); |
| 78 | + } catch (InterruptedException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + executorService.shutdown(); |
| 82 | + return result.get(); |
| 83 | + }; |
| 84 | + |
| 85 | + private static final Supplier<Integer> byJoin = () -> { |
| 86 | + AtomicInteger result = new AtomicInteger(); |
| 87 | + Thread thread = new Thread(() -> result.set(sum())); |
| 88 | + thread.start(); |
| 89 | + try { |
| 90 | + thread.join(); |
| 91 | + } catch (InterruptedException e) { |
| 92 | + e.printStackTrace(); |
| 93 | + } |
| 94 | + return result.get(); |
| 95 | + }; |
| 96 | + |
| 97 | + public static final Supplier<Integer> byCAS = () -> { |
| 98 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 99 | + AtomicBoolean isDone = new AtomicBoolean(false); |
| 100 | + AtomicInteger result = new AtomicInteger(); |
| 101 | + executorService.execute(() -> { |
| 102 | + result.set(sum()); |
| 103 | + isDone.set(true); |
| 104 | + }); |
| 105 | + while (!isDone.get()) { |
| 106 | + } |
| 107 | + executorService.shutdown(); |
| 108 | + return result.get(); |
| 109 | + }; |
| 110 | + |
| 111 | + public static final Supplier<Integer> byLock = () -> { |
| 112 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 113 | + Lock lock = new ReentrantLock(); |
| 114 | + Condition condition = lock.newCondition(); |
| 115 | + AtomicInteger result = new AtomicInteger(); |
| 116 | + AtomicBoolean isDone = new AtomicBoolean(false); |
| 117 | + executorService.execute(() -> { |
| 118 | + lock.lock(); |
| 119 | + try { |
| 120 | + result.set(sum()); |
| 121 | + isDone.set(true); |
| 122 | + condition.signalAll(); |
| 123 | + } finally { |
| 124 | + lock.unlock(); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + lock.lock(); |
| 129 | + try { |
| 130 | + while (!isDone.get()) { |
| 131 | + condition.await(); |
| 132 | + } |
| 133 | + condition.signalAll(); |
| 134 | + } catch (InterruptedException e) { |
| 135 | + e.printStackTrace(); |
| 136 | + } finally { |
| 137 | + lock.unlock(); |
| 138 | + } |
| 139 | + executorService.shutdown(); |
| 140 | + return result.get(); |
| 141 | + }; |
| 142 | + |
| 143 | + public static final Supplier<Integer> byObjectNotify = () -> { |
| 144 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 145 | + Object object = new Object(); |
| 146 | + AtomicInteger result = new AtomicInteger(); |
| 147 | + AtomicBoolean isDone = new AtomicBoolean(false); |
| 148 | + executorService.execute(() -> { |
| 149 | + synchronized (object) { |
| 150 | + result.set(sum()); |
| 151 | + isDone.set(true); |
| 152 | + object.notifyAll(); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + synchronized (object) { |
| 157 | + while (!isDone.get()) { |
| 158 | + try { |
| 159 | + object.wait(); |
| 160 | + } catch (InterruptedException e) { |
| 161 | + e.printStackTrace(); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + executorService.shutdown(); |
| 166 | + return result.get(); |
| 167 | + }; |
| 168 | + |
| 169 | + public static final Supplier<Integer> byCountDownLatch = () -> { |
| 170 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 171 | + CountDownLatch countDownLatch = new CountDownLatch(1); |
| 172 | + AtomicInteger result = new AtomicInteger(); |
| 173 | + executorService.execute(() -> { |
| 174 | + result.set(sum()); |
| 175 | + countDownLatch.countDown(); |
| 176 | + }); |
| 177 | + try { |
| 178 | + countDownLatch.await(); |
| 179 | + } catch (InterruptedException e) { |
| 180 | + e.printStackTrace(); |
| 181 | + } |
| 182 | + executorService.shutdown(); |
| 183 | + return result.get(); |
| 184 | + }; |
| 185 | + |
| 186 | + private static final Supplier<Integer> byFuture = () -> { |
| 187 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 188 | + Future<Integer> future = executorService.submit(()->sum()); |
| 189 | + Integer result = null; |
| 190 | + try { |
| 191 | + result = future.get(); |
| 192 | + } catch (InterruptedException | ExecutionException e) { |
| 193 | + e.printStackTrace(); |
| 194 | + } |
| 195 | + executorService.shutdown(); |
| 196 | + return result; |
| 197 | + }; |
| 198 | + |
| 199 | + private static final Supplier<Integer> byFutureTask = () -> { |
| 200 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 201 | + FutureTask<Integer> futureTask = new FutureTask<>(Homework03::sum); |
| 202 | + executorService.submit(futureTask); |
| 203 | + Integer result = null; |
| 204 | + try { |
| 205 | + result = futureTask.get(); |
| 206 | + } catch (InterruptedException | ExecutionException e) { |
| 207 | + e.printStackTrace(); |
| 208 | + } |
| 209 | + executorService.shutdown(); |
| 210 | + return result; |
| 211 | + }; |
| 212 | + |
| 213 | + public static final Supplier<Integer> byCompletableFuture = () -> { |
| 214 | + CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(Homework03::sum); |
| 215 | + return completableFuture.join(); |
| 216 | + }; |
| 217 | +} |
0 commit comments