forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinThreadTest.java
More file actions
39 lines (35 loc) · 1005 Bytes
/
JoinThreadTest.java
File metadata and controls
39 lines (35 loc) · 1005 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
package com.examplehub.basics.thread;
import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
class JoinThreadTest {
@Test
void test() throws InterruptedException {
Thread thread = new Thread(new JoinThread());
thread.start();
for (int i = 0; i < 10; i++) {
if (i == 1) {
thread.join();
}
System.out.println(Thread.currentThread().getName() + i);
}
}
@Test
void testJoinWithTime() throws InterruptedException {
AtomicInteger number = new AtomicInteger();
Thread thread =
new Thread(
() -> {
try {
Thread.sleep(100);
number.set(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread.start();
thread.join(50);
assertEquals(0, number.get());
assertEquals(Thread.State.TIMED_WAITING, thread.getState());
}
}