嗨,伙计们,所以我得到了这段代码
public class Padding {
static class Pair {
volatile long c1;
// UN-comment this line and see how perofmance is boosted * 2
// long q1; //Magic dodo thingy
volatile long c2;
}
static Pair p = new Pair();
static class Worker implements Runnable {
private static final int INT = Integer.MAX_VALUE/8;
private boolean b;
Worker(boolean b) {
this.b = b;
}
public void run() {
long start = System.currentTimeMillis();
if (b) {
for (int i = 0; i < INT; i++) {
p.c1++;
res += Math.random();
}
} else {
for (int i = 0; i < INT; i++) {
p.c2++;
res += Math.random();
}
}
long end = System.currentTimeMillis();
System.out.println("took: " + (end-start) + " Result:" + p.c1+p.c2);
}
}
public static void main(String[] args) {
System.out.println("Starting....");
Thread t1 = new Thread(new Worker(true));
Thread t2 = new Thread(new Worker(false));
t1.start();
t2.start();
}
}
因此,如果我运行它大约需要 11 秒,但如果我取消注释 qa1 它会在 3 秒内运行。我试图在互联网上找到一些东西,但没有足够的信息出现。据我了解,它与 JVM 优化有关,而长 q1 可能会使内存(或缓存)分布变得更好。无论如何,我的问题是有人知道我在哪里可以阅读更多有关它的信息。谢谢