第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

同步關(guān)鍵字Java速度效率

同步關(guān)鍵字Java速度效率

慕森王 2021-08-04 10:08:33
如果我錯(cuò)了,請(qǐng)隨時(shí)糾正我!java中的synchronized關(guān)鍵字使得一個(gè)方法不能同時(shí)在不同的線程中運(yùn)行。在我的程序中,我有 4 個(gè)不同的線程同時(shí)運(yùn)行,計(jì)數(shù)到 100.000。將 synchronized 關(guān)鍵字添加到正在執(zhí)行的方法中時(shí),它的時(shí)間應(yīng)該是多線程的四倍?無論以哪種方式執(zhí)行程序,大約需要 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();    }}線程調(diào)用的方法public class Even {        private int n = 0;//      public synchronized int next() {        public int next() {            n++;            n++;            return n;        }    }
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

微基準(zhǔn)測(cè)試是一個(gè)復(fù)雜的問題,因?yàn)橛绊憟?zhí)行時(shí)間的因素有很多(例如,即時(shí)編譯和垃圾收集)。評(píng)論部分已經(jīng)提供了一個(gè)很好的參考,但我建議您也看看我對(duì)類似問題的回答,該問題鏈接到Peter Setoft的外部資源,該資源提供了對(duì)微基準(zhǔn)測(cè)試的非常好的介紹以及需要做什么意識(shí)到。


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


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;

    }

}



查看完整回答
反對(duì) 回復(fù) 2021-08-04
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)