亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

同步關鍵字Java速度效率

同步關鍵字Java速度效率

慕森王 2021-08-04 10:08:33
如果我錯了,請隨時糾正我!java中的synchronized關鍵字使得一個方法不能同時在不同的線程中運行。在我的程序中,我有 4 個不同的線程同時運行,計數到 100.000。將 synchronized 關鍵字添加到正在執行的方法中時,它的時間應該是多線程的四倍?無論以哪種方式執行程序,大約需要 16 秒。這是我的代碼!public class ExerciseThree {    public static void main(String[] args) {        Even even = new Even();        Thread t1 = new Thread(() -> {            for (int i = 0; i < 100000; i++) {                System.out.println(even.next());            }        });        Thread t2 = new Thread(() -> {            for (int i = 0; i < 100000; i++) {                System.out.println(even.next());            }        });        Thread t3 = new Thread(() -> {            for (int i = 0; i < 100000; i++) {                System.out.println(even.next());            }        });        Thread t4 = new Thread(() -> {            for (int i = 0; i < 100000; i++) {                System.out.println(even.next());            }        });        System.out.println("starting thread 1");        t1.start();        System.out.println("starting thread 2");        t2.start();        System.out.println("starting thread 3");        t3.start();        System.out.println("starting thread 4");        t4.start();    }}線程調用的方法public class Even {        private int n = 0;//      public synchronized int next() {        public int next() {            n++;            n++;            return n;        }    }
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

微基準測試是一個復雜的問題,因為影響執行時間的因素有很多(例如,即時編譯和垃圾收集)。評論部分已經提供了一個很好的參考,但我建議您也看看我對類似問題的回答,該問題鏈接到Peter Setoft的外部資源,該資源提供了對微基準測試的非常好的介紹以及需要做什么意識到。


已經提到println()在這樣的微基準測試中沒有位置。此外,我想指出您應該使用某種同步機制(例如, a CountDownLatch)來確保四個線程同時開始執行它們的工作。創建和啟動線程所涉及的開銷可能會導致較早的線程在后面的線程啟動所需的時間內搶占先機,從而導致對even鎖的爭用比您預期的要少。例如,這可能看起來像這樣:


public class ExerciseThree {


    public static void main(String[] args) {

        final CountDownLatch startSignal = new CountDownLatch(1);

        final CountDownLatch threadReadyCheck = new CountDownLatch(4);

        final CountDownLatch threadDoneCheck = new CountDownLatch(4);

        Even even = new Even();

        Thread t1 = new Thread(() -> {

            threadReadyCheck.countDown();

            startSignal.await();

            for (int i = 0; i < 100000; i++) {

                even.next();

            }

            threadDoneCheck.countDown();

        });

        Thread t2 = new Thread(() -> {

            threadReadyCheck.countDown();

            startSignal.await();

            for (int i = 0; i < 100000; i++) {

                even.next();

            }

            threadDoneCheck.countDown();

        });

        Thread t3 = new Thread(() -> {

            threadReadyCheck.countDown();

            startSignal.await();

            for (int i = 0; i < 100000; i++) {

                even.next();

            }

            threadDoneCheck.countDown();

        });

        Thread t4 = new Thread(() -> {

            threadReadyCheck.countDown();

            startSignal.await();

            for (int i = 0; i < 100000; i++) {

                even.next();

            }

            threadDoneCheck.countDown();

        });

        t1.start();

        t2.start();

        t3.start();

        t4.start();

        // Wait until all threads are ready to perform their work.

        threadReadyCheck.await();

        // All threads ready.

        // This is where you log start time.

        long start = System.nanoTime();

        // Let threads progress to perform their actual work.

        startSignal.countDown();

        // Wait for threads to finish their work.

        threadDoneCheck.await();

        long end = System.nanoTime();

        // Note that this is again subject to many factors, for example when the main thread gets scheduled again after the workers terminate.

        long executionTime = end - start;

    }

}



查看完整回答
反對 回復 2021-08-04
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號