A, cache cache
Is between the cache and the main memory read and write block unit, designed to comply with the principle of locality is running - the principle of temporal locality and spatial locality principle (see "Principles of Computer Organization")
Two-dimensional array of the rows in the column faster than traversal, because two-dimensional array is stored in row, the cache block read from the main memory, the cache will write with counterparts of neighboring elements, resulting in the rows in the cache hit rate is greater than the column traversing the cache hit rate.
public class CacheTest { static final int LINE_NUM = 1024; static final int COLUMN_NUM = 1024; public static void main(String[] args){ long [][] array = new long[LINE_NUM][COLUMN_NUM]; long startTime = System.currentTimeMillis(); for (int i = 0; i < LINE_NUM; i++){//行遍历 for (int j = 0; j < COLUMN_NUM; j++){ array[i][j] = i*2 + j; } } long endTime = System.currentTimeMillis(); long cacheTime = endTime - startTime; System.out.println("cache time :" + cacheTime); startTime = System.currentTimeMillis(); for (int j = 0; j < COLUMN_NUM; j++){//列遍历 for (int i = 0; i < LINE_NUM; i++){ array[i][j] = i*2 + j; } } endTime = System.currentTimeMillis(); System.out.println("no cache time :" + (endTime - startTime)); } }
Second, the false sharing
False sharing: When a cache line, there are multiple variables, multiple threads can not modify update this multiple variables simultaneously. Resulting in a serial run multithreaded
/ * * False sharing method of a solution: cache behavior 64B, object header 8B, plus variable 8B, filled plus six invariant variable 48B, reaches a cache line corresponding to a variable * * / public Final static class FiledLong { public volatile Long value = 0L ; public Long P1, P2, P3, P4, P5, P6; } / * * method two false sharing solution: sun.misc.Contended comment * * / @Contended public Final static class FiledLong2 { public volatile Long value 0L = ; }