日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java中多线程的性能比较

發(fā)布時(shí)間:2023/12/3 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中多线程的性能比较 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Java中有多種用于多線程的技術(shù)。 可以通過同步關(guān)鍵字,鎖或原子變量來并行化Java中的一段代碼。 這篇文章將比較使用synced關(guān)鍵字ReentrantLock,getAndIncrement()以及執(zhí)行g(shù)et()和compareAndSet()調(diào)用的連續(xù)試驗(yàn)的性能。 創(chuàng)建了不同類型的Matrix類以進(jìn)行性能測試,其中還包括一個(gè)普通類。 為了進(jìn)行比較,在具有Intel Core I7(具有8個(gè)核心,其中4個(gè)是真實(shí)的),Ubuntu 14.04 LTS和Java的計(jì)算機(jī)上,對于不同大小的矩陣,具有不同類型的同步,線程數(shù)和池大小,所有單元都增加了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(); } } }

連續(xù)嘗試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; } } } } }

還為每個(gè)矩陣創(chuàng)建了工人類別。 這是普通工人階級:

/** * 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(); } } }

為了進(jìn)行正確的比較,默認(rèn)情況下,所有測試都會(huì)被重復(fù)20次。 計(jì)算每個(gè)結(jié)果的平均和標(biāo)準(zhǔn)誤差。 由于測試集有很多維度(矩陣類型,矩陣大小,池大小,線程數(shù)和經(jīng)過時(shí)間),因此某些功能在圖表中匯總顯示。 結(jié)果如下:對于池大小2和線程數(shù)2:

對于池大小4和線程數(shù)4:

對于池大小6和線程數(shù)6:

對于池大小8和線程數(shù)8:

對于池大小10和線程數(shù)10:

對于池大小12和線程數(shù)12:

結(jié)論

可以很容易地看到普通版本運(yùn)行最快。 但是,它不會(huì)產(chǎn)生預(yù)期的正確結(jié)果。 同步塊的性能更差(使用“ this ”完成同步時(shí))。 鎖比同步塊稍好。 但是,原子變量在所有變量中都明顯更好。 當(dāng)原子getAndIncrement以及對get()和compareAndSet()調(diào)用的連續(xù)試驗(yàn)進(jìn)行比較時(shí),表明它們的性能相同。 檢查Java源代碼時(shí),很容易理解其背后的原因:

/** * 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()進(jìn)行連續(xù)試驗(yàn)來實(shí)現(xiàn)getAndIncrement。 另一方面,當(dāng)檢查其他結(jié)果時(shí),可以看到池大小的影響。 當(dāng)使用的池大小小于實(shí)際線程數(shù)時(shí),將發(fā)生性能問題。 因此,Java中多線程的性能比較表明,當(dāng)確定要同步一段代碼并且出現(xiàn)性能問題時(shí),如果像測試中那樣使用此類線程,則應(yīng)嘗試使用Atomic變量。 其他選擇應(yīng)該是鎖或同步塊。 同樣,這并不意味著由于JIT編譯器的影響并且多次運(yùn)行一段代碼,同步塊總是比鎖更好。

  • 可以從此處下載用于Java多線程性能比較的源代碼: https : //github.com/kamaci/performance

翻譯自: https://www.javacodegeeks.com/2015/05/performance-comparison-of-multithreading-in-java.html

總結(jié)

以上是生活随笔為你收集整理的Java中多线程的性能比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。