同步关键字Java速度效率

如果我错了,请随时纠正我!


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;

        }

    }


慕森王
浏览 137回答 2
2回答

肥皂起泡泡

微基准测试是一个复杂的问题,因为影响执行时间的因素有很多(例如,即时编译和垃圾收集)。评论部分已经提供了一个很好的参考,但我建议您也看看我对类似问题的回答,该问题链接到Peter Setoft的外部资源,该资源提供了对微基准测试的非常好的介绍以及需要做什么意识到。已经提到println()在这样的微基准测试中没有位置。此外,我想指出您应该使用某种同步机制(例如, a CountDownLatch)来确保四个线程同时开始执行它们的工作。创建和启动线程所涉及的开销可能会导致较早的线程在后面的线程启动所需的时间内抢占先机,从而导致对even锁的争用比您预期的要少。例如,这可能看起来像这样:public class ExerciseThree {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; final CountDownLatch startSignal = new CountDownLatch(1);&nbsp; &nbsp; &nbsp; &nbsp; final CountDownLatch threadReadyCheck = new CountDownLatch(4);&nbsp; &nbsp; &nbsp; &nbsp; final CountDownLatch threadDoneCheck = new CountDownLatch(4);&nbsp; &nbsp; &nbsp; &nbsp; Even even = new Even();&nbsp; &nbsp; &nbsp; &nbsp; Thread t1 = new Thread(() -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadReadyCheck.countDown();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startSignal.await();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 100000; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; even.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadDoneCheck.countDown();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; Thread t2 = new Thread(() -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadReadyCheck.countDown();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startSignal.await();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 100000; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; even.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadDoneCheck.countDown();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; Thread t3 = new Thread(() -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadReadyCheck.countDown();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startSignal.await();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 100000; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; even.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadDoneCheck.countDown();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; Thread t4 = new Thread(() -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadReadyCheck.countDown();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startSignal.await();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 100000; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; even.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadDoneCheck.countDown();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; t1.start();&nbsp; &nbsp; &nbsp; &nbsp; t2.start();&nbsp; &nbsp; &nbsp; &nbsp; t3.start();&nbsp; &nbsp; &nbsp; &nbsp; t4.start();&nbsp; &nbsp; &nbsp; &nbsp; // Wait until all threads are ready to perform their work.&nbsp; &nbsp; &nbsp; &nbsp; threadReadyCheck.await();&nbsp; &nbsp; &nbsp; &nbsp; // All threads ready.&nbsp; &nbsp; &nbsp; &nbsp; // This is where you log start time.&nbsp; &nbsp; &nbsp; &nbsp; long start = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; // Let threads progress to perform their actual work.&nbsp; &nbsp; &nbsp; &nbsp; startSignal.countDown();&nbsp; &nbsp; &nbsp; &nbsp; // Wait for threads to finish their work.&nbsp; &nbsp; &nbsp; &nbsp; threadDoneCheck.await();&nbsp; &nbsp; &nbsp; &nbsp; long end = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; // Note that this is again subject to many factors, for example when the main thread gets scheduled again after the workers terminate.&nbsp; &nbsp; &nbsp; &nbsp; long executionTime = end - start;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java