Java中多线程的性能比较
Java中有多種用于多線程的技術。 可以通過同步關鍵字,鎖或原子變量來并行化Java中的一段代碼。 這篇文章將比較使用synced關鍵字ReentrantLock,getAndIncrement()以及執行get()和compareAndSet()調用的連續試驗的性能。 創建了不同類型的Matrix類以進行性能測試,其中還包括一個普通類。 為了進行比較,在具有Intel Core I7(具有8個核心,其中4個是真實的),Ubuntu 14.04 LTS和Java的計算機上,對于不同大小的矩陣,具有不同類型的同步,線程數和池大小,所有單元都增加了100倍。 1.7.0_60。
這是性能測試的簡單矩陣類:
/** * Plain matrix without synchronization. */ public class Matrix { private int rows; private int cols; private int[][] array; /** * Matrix constructor. * * @param rows number of rows * @param cols number of columns */ public Matrix(int rows, int cols) { this.rows = rows; this.cols = cols; array = new int[rows][rows]; } /** * Increments all matrix cells. */ public void increment() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { array[i][j]++; } } } /** * Returns a string representation of the object which shows row sums of each row. * * @return a string representation of the object. */ @Override public String toString() { StringBuffer s = new StringBuffer(); int rowSum; for (int i = 0; i < rows; i++) { rowSum = 0; for (int j = 0; j < cols; j++) { rowSum += array[i][j]; } s.append(rowSum); s.append(" "); } return s.toString(); } }對于其他矩陣,由于每種矩陣類型的剩余部分相同,因此列出了它們的增量方法。 同步矩陣:
public void increment() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { synchronized (this) { array[i][j]++; } } } }鎖矩陣:
public void increment() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { lock.lock(); try { array[i][j]++; } finally { lock.unlock(); } } } }原子getAndIncrement矩陣:
public void increment() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { array[i][j].getAndIncrement(); } } }連續嘗試get()和compareAndSet()矩陣:
public void increment() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { for (; ; ) { int current = array[i][j].get(); int next = current + 1; if (array[i][j].compareAndSet(current, next)) { break; } } } } }還為每個矩陣創建了工人類別。 這是普通工人階級:
/** * Worker for plain matrix without synchronization. * * @author Furkan KAMACI * @see Matrix */ public class PlainMatrixWorker extends Matrix implements Runnable { private AtomicInteger incrementCount = new AtomicInteger(WorkerDefaults.INCREMENT_COUNT); /** * Worker constructor. * * @param rows number of rows * @param cols number of columns */ public PlainMatrixWorker(int rows, int cols) { super(rows, cols); } /** * Increments matrix up to a maximum number. * * @see WorkerDefaults */ @Override public void run() { while (incrementCount.getAndDecrement() > 0) { increment(); } } }為了進行正確的比較,默認情況下,所有測試都會被重復20次。 計算每個結果的平均和標準誤差。 由于測試集有很多維度(矩陣類型,矩陣大小,池大小,線程數和經過時間),因此某些功能在圖表中匯總顯示。 結果如下:對于池大小2和線程數2:
對于池大小4和線程數4:
對于池大小6和線程數6:
對于池大小8和線程數8:
對于池大小10和線程數10:
對于池大小12和線程數12:
結論
可以很容易地看到普通版本運行最快。 但是,它不會產生預期的正確結果。 同步塊的性能更差(使用“ this ”完成同步時)。 鎖比同步塊稍好。 但是,原子變量在所有變量中都明顯更好。 當原子getAndIncrement以及對get()和compareAndSet()調用的連續試驗進行比較時,表明它們的性能相同。 檢查Java源代碼時,很容易理解其背后的原因:
/** * Atomically increments by one the current value. * * @return the previous value */ public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } }可以看出,在Java(1.7版)源代碼中,通過對get()和compareAndSet()進行連續試驗來實現getAndIncrement。 另一方面,當檢查其他結果時,可以看到池大小的影響。 當使用的池大小小于實際線程數時,將發生性能問題。 因此,Java中多線程的性能比較表明,當確定要同步一段代碼并且出現性能問題時,如果像測試中那樣使用此類線程,則應嘗試使用Atomic變量。 其他選擇應該是鎖或同步塊。 同樣,這并不意味著由于JIT編譯器的影響并且多次運行一段代碼,同步塊總是比鎖更好。
- 可以從此處下載用于Java多線程性能比較的源代碼: https : //github.com/kamaci/performance
翻譯自: https://www.javacodegeeks.com/2015/05/performance-comparison-of-multithreading-in-java.html
總結
以上是生活随笔為你收集整理的Java中多线程的性能比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: apache shiro_Apache
- 下一篇: Spring整合基础