|
3 | 3 | import java.util.LinkedList;
|
4 | 4 | import java.util.List;
|
5 | 5 | import java.util.UUID;
|
| 6 | +import java.util.concurrent.Callable; |
6 | 7 | import java.util.concurrent.ExecutionException;
|
7 | 8 | import java.util.concurrent.ExecutorService;
|
8 | 9 | import java.util.concurrent.Executors;
|
|
15 | 16 | *
|
16 | 17 | * Executors help us to decouple task submission from execution.
|
17 | 18 | *
|
18 |
| - * We have 4 types of executors: |
| 19 | + * We have 6 types of executors: |
19 | 20 | *
|
20 | 21 | * - Single Thread Executor: Uses a single worker to process tasks.
|
21 | 22 | *
|
|
27 | 28 | *
|
28 | 29 | * - Scheduled Thread Pool: Bounded thread limit, used for delayed tasks.
|
29 | 30 | *
|
| 31 | + * - Single-Thread Scheduled Pool: Similar to the scheduled thread pool, but |
| 32 | + * single-threaded, with only one active task at the time. |
| 33 | + * |
| 34 | + * - Work-Stealing Thread Pool: Based on Fork/Join Framework, applies the |
| 35 | + * work-stealing algorithm for balancing tasks, with available processors as a |
| 36 | + * paralellism level. |
| 37 | + * |
30 | 38 | * And 2 types of tasks:
|
31 | 39 | *
|
32 | 40 | * - execute: Executes without giving feedback. Fire-and-forget.
|
@@ -117,23 +125,77 @@ public static void usingFixedThreadPool() {
|
117 | 125 | public static void usingScheduledThreadPool() {
|
118 | 126 | System.out.println("=== ScheduledThreadPool ===");
|
119 | 127 | ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(4);
|
120 |
| - scheduledThreadPool.scheduleAtFixedRate( |
121 |
| - () -> System.out.println("Print every 2s"), 0, 2, TimeUnit.SECONDS); |
122 |
| - scheduledThreadPool.scheduleWithFixedDelay( |
123 |
| - () -> System.out.println("Print every 2s delay"), 0, 2, TimeUnit.SECONDS); |
| 128 | + scheduledThreadPool.scheduleAtFixedRate(() -> System.out.println("1) Print every 2s"), 0, 2, TimeUnit.SECONDS); |
| 129 | + scheduledThreadPool.scheduleAtFixedRate(() -> System.out.println("2) Print every 2s"), 0, 2, TimeUnit.SECONDS); |
| 130 | + scheduledThreadPool.scheduleWithFixedDelay(() -> System.out.println("3) Print every 2s delay"), 0, 2, |
| 131 | + TimeUnit.SECONDS); |
124 | 132 |
|
125 | 133 | try {
|
126 | 134 | scheduledThreadPool.awaitTermination(6, TimeUnit.SECONDS);
|
127 | 135 | scheduledThreadPool.shutdown();
|
128 | 136 | } catch (InterruptedException e) {
|
129 | 137 | e.printStackTrace();
|
130 | 138 | }
|
| 139 | + System.out.println("\n\n"); |
| 140 | + } |
| 141 | + |
| 142 | + public static void usingSingleTreadScheduledExecutor() { |
| 143 | + System.out.println("=== SingleThreadScheduledThreadPool ==="); |
| 144 | + ScheduledExecutorService singleThreadScheduler = Executors.newSingleThreadScheduledExecutor(); |
| 145 | + singleThreadScheduler.scheduleAtFixedRate(() -> System.out.println("1) Print every 2s"), 0, 2, TimeUnit.SECONDS); |
| 146 | + singleThreadScheduler.scheduleWithFixedDelay(() -> System.out.println("2) Print every 2s delay"), 0, 2, |
| 147 | + TimeUnit.SECONDS); |
| 148 | + |
| 149 | + try { |
| 150 | + singleThreadScheduler.awaitTermination(6, TimeUnit.SECONDS); |
| 151 | + singleThreadScheduler.shutdown(); |
| 152 | + } catch (InterruptedException e) { |
| 153 | + e.printStackTrace(); |
| 154 | + } |
| 155 | + System.out.println("\n\n"); |
| 156 | + |
131 | 157 | }
|
132 | 158 |
|
| 159 | + public static void usingWorkStealingThreadPool() { |
| 160 | + System.out.println("=== WorkStealingThreadPool ==="); |
| 161 | + ExecutorService workStealingPool = Executors.newWorkStealingPool(); |
| 162 | + |
| 163 | + workStealingPool.execute(() -> System.out.println("Prints normally")); |
| 164 | + |
| 165 | + Callable<UUID> generatesUUID = UUID::randomUUID; |
| 166 | + List<Callable<UUID>> severalUUIDsTasks = new LinkedList<>(); |
| 167 | + for (int i = 0; i < 20; i++) { |
| 168 | + severalUUIDsTasks.add(generatesUUID); |
| 169 | + } |
| 170 | + |
| 171 | + try { |
| 172 | + List<Future<UUID>> futureUUIDs = workStealingPool.invokeAll(severalUUIDsTasks); |
| 173 | + for (Future<UUID> future : futureUUIDs) { |
| 174 | + if (future.isDone()) { |
| 175 | + UUID uuid = future.get(); |
| 176 | + System.out.println("New UUID :" + uuid); |
| 177 | + } |
| 178 | + } |
| 179 | + } catch (InterruptedException | ExecutionException e) { |
| 180 | + e.printStackTrace(); |
| 181 | + } |
| 182 | + try { |
| 183 | + workStealingPool.awaitTermination(6, TimeUnit.SECONDS); |
| 184 | + workStealingPool.shutdown(); |
| 185 | + } catch (InterruptedException e) { |
| 186 | + e.printStackTrace(); |
| 187 | + } |
| 188 | + System.out.println("\n\n"); |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + |
133 | 193 | public static void main(String[] args) {
|
134 | 194 | usingSingleThreadExecutor();
|
135 | 195 | usingCachedThreadPool();
|
136 | 196 | usingFixedThreadPool();
|
137 | 197 | usingScheduledThreadPool();
|
| 198 | + usingSingleTreadScheduledExecutor(); |
| 199 | + usingWorkStealingThreadPool(); |
138 | 200 | }
|
139 | 201 | }
|
0 commit comments