-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithreadingCompleteTutorial.java
More file actions
167 lines (152 loc) · 6.83 KB
/
Copy pathMultithreadingCompleteTutorial.java
File metadata and controls
167 lines (152 loc) · 6.83 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package Phase7_Concurrency.Multithreading;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
/**
* Multithreading Complete Tutorial
* --------------------------------
* One file, end-to-end, touching every major topic in the section.
* Pair this with the dedicated files for depth.
* <p>
*
* Table of contents
* -----------------
* 1. Spawning a thread (Threads, Runnable)
* 2. Lifecycle states (ThreadLifecycle)
* 3. start vs run (StartVsRun)
* 4. Join / sleep / interrupt (ThreadJoinMethod, ThreadSleepMethod, ThreadInterruption)
* 5. Priority / daemon (ThreadPriority, DaemonThread)
* 6. Synchronized + thread safety (JavaSynchronization, ThreadSafety)
* 7. volatile + JMM (VolatileKeyword, JavaMemoryModel)
* 8. wait/notify + producer/consumer (WaitNotifyNotifyAll, ProducerConsumer)
* 9. ThreadLocal (ThreadLocalDemo)
* 10. Atomics (AtomicVariables)
* 11. Locks + Conditions (LocksInJava, ReentrantLockDemo)
* 12. Read/Write + Stamped (ReadWriteLockDemo, StampedLockDemo)
* 13. Deadlock + livelock (DeadlockDemo, RaceConditionStarvationLivelock)
* 14. Thread pools / Executor (ThreadPools, ExecutorFramework)
* 15. Scheduled / ForkJoin (ScheduledExecutorDemo, ForkJoinPoolDemo)
* 16. CompletableFuture (CompletableFutureDemo)
* 17. Synchronizers (CountDownLatch, CyclicBarrier, Semaphore, Phaser)
* 18. Java 21: Virtual threads (VirtualThreads)
* 19. Java 21: Structured concurrency / Scoped values
* 20. Putting it together: tiny pipeline
*/
public class MultithreadingCompleteTutorial {
public static void main(String[] args) throws Exception {
section("1) Spawn a thread");
Thread t = new Thread(() ->
System.out.println("hi from " + Thread.currentThread().getName()), "first");
t.start();
t.join();
section("2) Lifecycle snapshot");
Thread sleeper = new Thread(() -> {
try { Thread.sleep(80); } catch (InterruptedException ignored) {}
}, "sleeper");
sleeper.start();
Thread.sleep(20);
System.out.println("sleeper state = " + sleeper.getState()); // TIMED_WAITING
sleeper.join();
System.out.println("after join, state = " + sleeper.getState()); // TERMINATED
section("3) Synchronized + race condition fix");
AtomicInteger unsafe = new AtomicInteger();
Object lock = new Object();
int[] safe = { 0 };
Runnable mix = () -> {
for (int i = 0; i < 50_000; i++) {
unsafe.incrementAndGet(); // atomic
synchronized (lock) { safe[0]++; } // synchronized
}
};
Thread a = new Thread(mix), b = new Thread(mix);
a.start(); b.start(); a.join(); b.join();
System.out.println("atomic = " + unsafe.get() + ", sync = " + safe[0]);
section("4) Producer / consumer with wait/notifyAll");
var buf = new java.util.LinkedList<Integer>();
int CAP = 3;
Thread prod = new Thread(() -> {
try {
for (int i = 1; i <= 5; i++) {
synchronized (buf) {
while (buf.size() == CAP) buf.wait();
buf.add(i); System.out.println(" put " + i);
buf.notifyAll();
}
Thread.sleep(10);
}
} catch (InterruptedException ie) { Thread.currentThread().interrupt(); }
});
Thread cons = new Thread(() -> {
try {
for (int i = 1; i <= 5; i++) {
int v;
synchronized (buf) {
while (buf.isEmpty()) buf.wait();
v = buf.remove();
buf.notifyAll();
}
System.out.println(" got " + v);
Thread.sleep(20);
}
} catch (InterruptedException ie) { Thread.currentThread().interrupt(); }
});
prod.start(); cons.start(); prod.join(); cons.join();
section("5) ReentrantLock + tryLock");
ReentrantLock rl = new ReentrantLock();
rl.lock();
try { System.out.println("got it, holdCount=" + rl.getHoldCount()); }
finally { rl.unlock(); }
section("6) Executor framework + Future");
try (ExecutorService es = Executors.newFixedThreadPool(2)) {
int sum = es.submit(() -> {
int s = 0; for (int i = 1; i <= 100; i++) s += i; return s;
}).get();
System.out.println("sum 1..100 = " + sum);
}
section("7) Synchronizers — CountDownLatch + Semaphore");
CountDownLatch start = new CountDownLatch(1);
Semaphore slots = new Semaphore(2);
Thread[] hot = new Thread[5];
for (int i = 0; i < hot.length; i++) {
final int id = i;
hot[i] = new Thread(() -> {
try {
start.await();
slots.acquire();
try {
System.out.println(" task " + id + " in critical section");
Thread.sleep(30);
} finally { slots.release(); }
} catch (InterruptedException ie) { Thread.currentThread().interrupt(); }
});
hot[i].start();
}
Thread.sleep(50);
start.countDown(); // fire the starting gun
for (Thread x : hot) x.join();
section("8) CompletableFuture pipeline");
CompletableFuture<Integer> pipeline = CompletableFuture
.supplyAsync(() -> 21)
.thenApply(n -> n * 2)
.thenApply(n -> n + 0);
System.out.println("pipeline = " + pipeline.get());
section("9) Virtual threads — 5000 sleeping in parallel");
long t0 = System.currentTimeMillis();
try (ExecutorService vts = Executors.newVirtualThreadPerTaskExecutor()) {
List<CompletableFuture<Void>> jobs = new java.util.ArrayList<>();
for (int i = 0; i < 5_000; i++) {
jobs.add(CompletableFuture.runAsync(() -> {
try { Thread.sleep(50); } catch (InterruptedException ignored) {}
}, vts));
}
for (CompletableFuture<Void> f : jobs) f.get();
}
System.out.println("5000 virtual sleepers done in " + (System.currentTimeMillis() - t0) + " ms");
section("done");
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}