-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSleepMethod.java
More file actions
121 lines (109 loc) · 4.31 KB
/
Copy pathThreadSleepMethod.java
File metadata and controls
121 lines (109 loc) · 4.31 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
package Phase7_Concurrency.Multithreading;
import java.time.Duration;
import java.time.Instant;
/**
* Thread.sleep(...)
* -----------------
* Static method on Thread that PAUSES the CURRENT thread for at least
* the specified amount of time. Two overloads:
* <p>
*
* Thread.sleep(long millis)
* Thread.sleep(long millis, int nanos)
* Thread.sleep(Duration d) // Java 19+
* <p>
*
* Key facts
* ---------
* 1. Sleep is for the CURRENT thread only. There is no "sleep that
* other thread" call. If you need that, signal it and have IT call
* sleep / wait on something.
* <p>
*
* 2. Sleep gives up the CPU. The thread enters TIMED_WAITING.
* <p>
*
* 3. Sleep does NOT release locks. If you fell asleep inside a
* synchronized block you still own the monitor — every other thread
* waiting on it stays blocked.
* <p>
*
* 4. Sleep is INTERRUPTIBLE. If another thread calls .interrupt() on
* this thread, sleep throws InterruptedException and clears the
* interrupt flag. You should usually restore it.
* <p>
*
* 5. Sleep duration is a LOWER BOUND, not exact. You can sleep longer
* than requested (OS scheduling, GC, etc.) but not less.
* <p>
*
* What sleep is NOT for
* ---------------------
* - Waiting for a condition: use wait()/notify or LockSupport.park or
* a CountDownLatch.
* - Throttling: use a real rate limiter or Semaphore.
* - Coordination: use join() / barriers / latches.
* <p>
*
* Polling with sleep is rarely correct and almost always wasteful.
*/
public class ThreadSleepMethod {
public static void main(String[] args) throws InterruptedException {
section("1) Basic sleep");
Instant before = Instant.now();
Thread.sleep(100);
System.out.println("slept ~" + Duration.between(before, Instant.now()).toMillis() + " ms");
section("2) Duration overload (Java 19+)");
Thread.sleep(Duration.ofMillis(150));
System.out.println("Duration.ofMillis(150) — done");
section("3) sleep is interruptible");
Thread t = new Thread(() -> {
try {
Thread.sleep(5_000);
System.out.println("WOULD have slept 5s");
} catch (InterruptedException ie) {
System.out.println("interrupted! flag is now = " + Thread.currentThread().isInterrupted());
// Best practice: re-set the flag because catch CLEARED it.
Thread.currentThread().interrupt();
System.out.println("flag restored = " + Thread.currentThread().isInterrupted());
}
}, "sleeper");
t.start();
Thread.sleep(100); // let it enter sleep
t.interrupt();
t.join();
section("4) sleep does NOT release locks");
Object lock = new Object();
Thread holder = new Thread(() -> {
synchronized (lock) {
System.out.println("holder acquired the lock, now sleeping for 200ms");
try { Thread.sleep(200); } catch (InterruptedException ignored) {}
System.out.println("holder releasing lock");
}
}, "holder");
Thread waiter = new Thread(() -> {
long t0 = System.currentTimeMillis();
synchronized (lock) {
System.out.println("waiter got lock after " + (System.currentTimeMillis() - t0) + " ms");
}
}, "waiter");
holder.start();
Thread.sleep(30); // ensure holder grabs first
waiter.start();
holder.join(); waiter.join();
section("5) Sleeping zero — like yield");
Thread.sleep(0); // not a no-op; gives the scheduler a chance
System.out.println("Thread.sleep(0) returned");
section("6) sleep is for the CURRENT thread");
// There is no Thread#sleepOther — you cannot directly pause another
// thread. You'd have to cooperate (signal it via a flag, then it
// chooses to sleep).
Thread other = new Thread(() -> System.out.println("running normally"));
other.start(); other.join();
section("done");
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}