-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreadLocalTest.java
executable file
·48 lines (42 loc) · 1.15 KB
/
ThreadLocalTest.java
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
package tests.java.lang;
/*
* #%L
* Evaluation
* %%
* Copyright (C) 2014 Github jjYBdx4IL Projects
* %%
* #L%
*/
import static org.junit.Assert.*;
import org.junit.Test;
public class ThreadLocalTest {
@Test
public void test1() throws InterruptedException {
final ThreadLocal<String> threadLocalMap = new ThreadLocal<>();
threadLocalMap.set("1");
Thread t = new Thread() {
@Override
public void run() {
threadLocalMap.set("2");
}
};
t.start();
t.join();
assertEquals("1", threadLocalMap.get());
}
@Test
public void testInitialValueNoUserDef() throws InterruptedException {
final ThreadLocal<String> strings = new ThreadLocal<>();
assertNull(strings.get());
}
@Test
public void testInitialValueUserDef() throws InterruptedException {
final ThreadLocal<String> strings = new ThreadLocal<String>() {
@Override protected String initialValue() {
return "abc";
}
};
assertNotNull(strings.get());
assertEquals("abc", strings.get());
}
}