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();
return new Properties(properties);
}
};
}
@Test
public void testOne() throws Exception {
Thread t1 = new Thread(() -> {
threadLocal.get().setProperty("Two", "two");
System.out.println(Thread.currentThread().getName() + threadLocal.get());
System.out.println(Thread.currentThread().getName() + threadLocal.get().getProperty("test"));
});
Thread t2 = new Thread(() -> {
threadLocal.get().setProperty("Two", "three");
System.out.println(Thread.currentThread().getName() + threadLocal.get());
System.out.println(Thread.currentThread().getName() + threadLocal.get().getProperty("test"));
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(Thread.currentThread().getName() + threadLocal.get());
System.out.println(Thread.currentThread().getName() + threadLocal.get().getProperty("test"));
}
}