import org junit Before import org junit Test import java util Propert

 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
import org.junit.Before;
import org.junit.Test;
import java.util.Properties;
/**
* Created by dzirtbry on 6/24/15.
*/
public class ThreadsTest {
Properties properties;
ThreadLocal<Properties> threadLocal;
@Before
public void setUp() throws Exception {
properties = new Properties();
properties.setProperty("test", "one");
threadLocal = new ThreadLocal<Properties>() {
@Override
protected Properties initialValue() {
return (Properties) properties.clone();
}
};
}
@Test
public void testOne() throws Exception {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
threadLocal.get().setProperty("Two", "two");
System.out.println(Thread.currentThread().getName() + threadLocal.get());
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
threadLocal.get().setProperty("Two", "three");
System.out.println(Thread.currentThread().getName() + threadLocal.get());
}
});
t1.start();
t2.start();
System.out.println(Thread.currentThread().getName() + threadLocal.get());
}
}