-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads.java
More file actions
46 lines (39 loc) · 902 Bytes
/
Threads.java
File metadata and controls
46 lines (39 loc) · 902 Bytes
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
class ExampleThread extends Thread {
ExampleThread(String s) {
super(s);
}
static ExampleThread createAndStart(String s) {
var t = new ExampleThread(s);
t.start();
return t;
}
public void run() {
for (int i = 1; i < 5; i++) {
System.out.print(getName() + " starting run " + i);
try {
sleep(500);
} catch (InterruptedException e) {
System.out.println("Error: " + e);
}
System.out.println("\n" + getName() + " ending run " + i);
}
}
}
class Threads {
public static void main(String[] sth) {
threading();
}
static void threading() {
try {
var t = ExampleThread.createAndStart("Child");
for (int i = 0; i < 5; i++) {
System.out.print("*");
Thread.sleep(300);
}
t.join();
} catch (InterruptedException e) {
System.out.println("Error: " + e);
}
System.out.println(Thread.currentThread().getName() + " thread ending..");
}
}