Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created November 19, 2011 09:43
Show Gist options
  • Save j5ik2o/1378661 to your computer and use it in GitHub Desktop.
Save j5ik2o/1378661 to your computer and use it in GitHub Desktop.
リオーダーテスト
package test;
import java.util.concurrent.CountDownLatch;
public class ReOrderingTest {
static int x = 0;
static int y = 0;
static int a = 0;
static int b = 0;
static CountDownLatch startLatch;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 1000000; i++) {
x = 0;
y = 0;
a = 0;
b = 0;
startLatch = new CountDownLatch(1);
Thread aThread = new Thread(new Runnable() { // スレッドA
@Override
public void run() {
try {
startLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
a = 1;
x = b;
}
});
Thread bThread = new Thread(new Runnable() { // スレッドB
@Override
public void run() {
try {
startLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
b = 1;
y = a;
}
});
aThread.start();
bThread.start();
try {
startLatch.countDown();
aThread.join();
bThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i + " (" + x + " , " + y + ")");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment