public class VolatileTest {
private static class T {
public long p1,p2,p3, p4,p5;// if comment this and run again
public long x = 0L;
public long y = 0L;
}
public static T[] arr = new T[2];
static {
arr[0] = new T();
arr[1] = new T();
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(()->{
for(long a=0;a<999999999L;a++){
arr[0].x = a;
}
});
Thread t2 = new Thread(()->{
for(long a=0;a<999999999L;a++){
arr[1].y = a;
}
});
final long start = System.nanoTime();
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println((System.nanoTime()-start)/1000000);
}}
the first time I run above code, it cost 519. If i comment line 3 and run again, it cost 510. Why the second code (wihout padding) run faster.thanks