java model.put_深入理解Java内存模型(一)——基础
偶豐富了一下上邊提到的例子:
package test034;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ReorderTest {
private int a = 0;
private int b = 0;
public static void main(String[] args) throws InterruptedException,
ExecutionException {
ReorderTest rt = new ReorderTest();
rt.doTest();
}
public void doTest() throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(2);
//可能出現(xiàn)的結(jié)果(關(guān)注ThreadATask,ThreadBTask取到的結(jié)果)
int num00 = 0; //x=0,y=0
int num10 = 0; //x=1,y=0
int num02 = 0; //x=0,y=2
int num12 = 0; //x=1,y=2
//可能出現(xiàn)的結(jié)果(關(guān)注主線程取到的結(jié)果)
int main00_a = 0;
int main10_a = 0;
int main02_a = 0;
int main12_a = 0;
//可能出現(xiàn)的結(jié)果(關(guān)注主線程取到的結(jié)果)
int main00_b = 0;
int main10_b = 0;
int main02_b = 0;
int main12_b = 0;
int allNum = 100000;
for (int i = 0; i < allNum; i++) {
CountDownLatch latch = new CountDownLatch(1);
Future futureA = executor.submit(new ThreadATask(latch));
Future futureB = executor.submit(new ThreadBTask(latch));
latch.countDown();
if (a==0 && b==0) {main00_a++;}
if (a==1 && b==0) {main10_a++;}
if (a==0 && b==2) {main02_a++;}
if (a==1 && b==2) {main12_a++;}
int x = (Integer) futureB.get(); //取得a
int y = (Integer) futureA.get(); //取得b
if (x==0 && y==0) {num00++;}
if (x==1 && y==0) {num10++;}
if (x==0 && y==2) {num02++;}
if (x==1 && y==2) {num12++;}
if (a==0 && b==0) {main00_b++;}
if (a==1 && b==0) {main10_b++;}
if (a==0 && b==2) {main02_b++;}
if (a==1 && b==2) {main12_b++;}
//重置狀態(tài)
a = 0;
b = 0;
}
System.out.println("a=0,b=0出現(xiàn)次數(shù):" + main00_a);
System.out.println("a=1,b=0出現(xiàn)次數(shù):" + main10_a);
System.out.println("a=0,b=2出現(xiàn)次數(shù):" + main02_a);
System.out.println("a=1,b=2出現(xiàn)次數(shù):" + main12_a);
System.out.println("");
System.out.println("x=0,y=0出現(xiàn)次數(shù):" + num00);
System.out.println("x=1,y=0出現(xiàn)次數(shù):" + num10);
System.out.println("x=0,y=2出現(xiàn)次數(shù):" + num02);
System.out.println("x=1,y=2出現(xiàn)次數(shù):" + num12);
System.out.println("");
System.out.println("a=0,b=0出現(xiàn)次數(shù):" + main00_b);
System.out.println("a=1,b=0出現(xiàn)次數(shù):" + main10_b);
System.out.println("a=0,b=2出現(xiàn)次數(shù):" + main02_b);
System.out.println("a=1,b=2出現(xiàn)次數(shù):" + main12_b);
executor.shutdown();
}
private class ThreadATask implements Callable {
private CountDownLatch barrier;
public ThreadATask(CountDownLatch barrier) {
this.barrier = barrier;
}
public Integer call() throws InterruptedException,
BrokenBarrierException {
barrier.await(); //
a = 1; // A1
return b; // A2
}
}
private class ThreadBTask implements Callable {
private CountDownLatch barrier;
public ThreadBTask(CountDownLatch barrier) {
this.barrier = barrier;
}
public Integer call() throws InterruptedException,
BrokenBarrierException {
barrier.await();
b = 2; // B1
return a; // B2
}
}
}
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的java model.put_深入理解Java内存模型(一)——基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java lock 对象_Java并发编
- 下一篇: java8 map reduce 分组_