-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads.java
More file actions
147 lines (132 loc) · 5.59 KB
/
Copy pathThreads.java
File metadata and controls
147 lines (132 loc) · 5.59 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
package Phase7_Concurrency.Multithreading;
/**
* java.lang.Thread
* ----------------
* Thread is the JVM-level representation of a thread of execution.
* Every Java program starts with one Thread (the "main" thread). All
* other threads must be CREATED explicitly.
* <p>
*
* Two Ways To Define Work
* -----------------------
* 1. EXTEND Thread - override run(). Couples your code to
* the Thread class. Cannot also extend
* another class.
* <p>
*
* 2. IMPLEMENT Runnable - pass to a Thread / ExecutorService.
* PREFERRED — separates the work from
* the mechanism. Works with thread pools.
* <p>
*
* Important Constructors
* ----------------------
* new Thread() - no-op run() by default.
* new Thread(Runnable r) - run r.run() on a new thread.
* new Thread(Runnable r, String name) - give it a debugging-friendly name.
* new Thread(ThreadGroup g, Runnable r) - join a thread group (rarely used).
* <p>
*
* Important Instance Methods
* --------------------------
* start() - schedules the thread and calls run() on it.
* Throwing IllegalThreadStateException if called twice.
* run() - the work itself. DO NOT CALL DIRECTLY — that runs on
* the current thread, not a new one.
* join() - wait for the thread to finish.
* interrupt() - request the thread to stop (cooperative).
* isAlive() - has it started and not finished?
* getId() - JVM-unique id for this thread.
* getName() / setName(...)
* getPriority() / setPriority(int)
* setDaemon(boolean) - daemon threads don't keep the JVM alive.
* getState() - lifecycle state (NEW, RUNNABLE, ...).
* <p>
*
* Important Static Methods
* ------------------------
* currentThread() - the running thread.
* sleep(ms) - pause this thread (releases CPU, KEEPS LOCKS).
* yield() - hint to the scheduler: I'm OK to wait.
* onSpinWait() - hint inside a busy-wait loop (Java 9+).
* activeCount() - number of active threads in the current group.
* <p>
*
* Static Methods Java 19+ — Virtual Threads
* -----------------------------------------
* ofPlatform() - builder for a classic OS-backed thread.
* ofVirtual() - builder for a virtual (user-mode) thread.
* startVirtualThread(Runnable) - one-shot virtual thread.
* <p>
*
* Naming, IDs, Equality
* ---------------------
* Two Thread objects are equal only via reference equality (==). Names
* are NOT unique. Always set a meaningful name — it shows up in stack
* traces and profilers.
*/
public class Threads {
public static void main(String[] args) throws InterruptedException {
section("1) Extending Thread");
MyThread t1 = new MyThread("ext-1");
t1.start();
t1.join();
section("2) Implementing Runnable");
// Same work, but composable with thread pools, lambdas, etc.
Thread t2 = new Thread(new MyTask(), "run-1");
t2.start();
t2.join();
section("3) Runnable as a lambda");
Thread t3 = new Thread(() -> System.out.println("lambda task on " +
Thread.currentThread().getName()), "lambda-1");
t3.start();
t3.join();
section("4) Thread identity and properties");
Thread t4 = new Thread(() -> {}, "props");
System.out.println("id = " + t4.threadId()); // Java 19+ replaces getId
System.out.println("name = " + t4.getName());
System.out.println("priority = " + t4.getPriority());
System.out.println("daemon = " + t4.isDaemon());
System.out.println("alive = " + t4.isAlive());
System.out.println("state = " + t4.getState());
section("5) Inspect the main thread");
Thread main = Thread.currentThread();
System.out.println(main.getName() + " state = " + main.getState() +
", priority = " + main.getPriority() +
", daemon = " + main.isDaemon());
section("6) Calling start() twice throws IllegalThreadStateException");
Thread once = new Thread(() -> {}, "once");
once.start();
once.join();
try { once.start(); }
catch (IllegalThreadStateException ex) {
System.out.println("expected: " + ex);
}
section("7) Java 21 platform vs virtual builders");
Thread platform = Thread.ofPlatform().name("platform-1").unstarted(() ->
System.out.println("hi from " + Thread.currentThread()));
platform.start();
platform.join();
Thread virtual = Thread.ofVirtual().name("virt-1").start(() ->
System.out.println("hi from " + Thread.currentThread()));
virtual.join();
section("done");
}
/** Variant 1: extending Thread directly. */
static class MyThread extends Thread {
MyThread(String name) { super(name); }
@Override public void run() {
System.out.println("MyThread running on " + getName());
}
}
/** Variant 2: implementing Runnable — preferred in modern Java. */
static class MyTask implements Runnable {
@Override public void run() {
System.out.println("MyTask running on " + Thread.currentThread().getName());
}
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}