Last active
August 29, 2015 14:12
-
-
Save lispmind/f410eb3500f288bbef9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import java.util.concurrent.CountDownLatch; | |
public class ThreadTest { | |
@Test | |
public void testInit() { | |
for (int i = 0; i < 10; i++) { | |
ThreadInfo info = new ThreadInfo(); | |
info.set(true); | |
} | |
} | |
@Test | |
public void testThread() throws InterruptedException { | |
int max = 5; | |
final CountDownLatch countDownLatch = new CountDownLatch(max); | |
for (int i = 0; i < max; i++) { | |
new Thread() { | |
@Override | |
public void run() { | |
for (int j = 0; j < 10; j++) { | |
ThreadInfo info = new ThreadInfo(); | |
info.set(true); | |
} | |
countDownLatch.countDown(); | |
} | |
}.start(); | |
} | |
countDownLatch.await(); | |
} | |
} | |
class ThreadInfo { | |
private static ThreadLocal<Boolean> test = new ThreadLocal<Boolean>() { | |
@Override | |
protected Boolean initialValue() { | |
System.out.println(Thread.currentThread().getId() + " init false"); | |
return false; | |
} | |
}; | |
public void set(boolean value) { | |
if (test.get() != value) { | |
System.out.println(Thread.currentThread().getId() + " change " + test.get() + " -> " + value); | |
} | |
test.set(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment