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());
}
}