synchronized 对性能的影响

 
public static void main(String[] args) throws Exception {

    Runnable run1 = new Runnable() {
        public void run() {
            testCase(1);
        }
    };
    Runnable run2 = new Runnable() {
        public void run() {
            testCase(2);
        }
    };
    Runnable run3 = new Runnable() {
        public void run() {
            testCase(3);
        }
    };
    Runnable run4 = new Runnable() {
        public void run() {
            testCase(4);
        }
    };

    new Thread(run1).start();
//        new Thread(run2).start();
//        new Thread(run3).start();
//        new Thread(run4).start();
}

public static void testCase(int tid) {
    int n = 10000;
    for (int j = 0; j < 1; j++) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {

            nTestProcess();

        }
        System.out.println("normal test" + tid + " "
                + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            sTestProcess();
        }
        System.out.println("synchronized test" + tid + " "
                + (System.currentTimeMillis() - start));
    }
}

public static void testProcess() {
    int c = 1;
    for (int i = 1; i < 10000; i++) {
        c *= i;
        if (c % 2 == 0) {
            c += i;
        }
        if (i == 0) {
            throw new RuntimeException();
        }
    }
}

public static void nTestProcess() {
    testProcess();
}

public static synchronized void sTestProcess() {
    testProcess();
}

 

 

注意上面加红的代码

结果如下:

normal test1 318
synchronized test1 291

将加红代码注释去掉一个,结果如下:

normal test2 336
normal test1 348
synchronized test2 617
synchronized test1 631

加红代码注释去掉两个,结果如下:

normal test2 362
normal test3 392
normal test1 399
synchronized test3 892
synchronized test2 936
synchronized test1 935

加红代码注释全部去掉,结果如下:

normal test2 416
normal test4 420
normal test1 427
normal test3 436
synchronized test3 992
synchronized test2 1077
synchronized test4 1114
synchronized test1 1173

  

 

结论:直观结论时如果单线程运行时,锁机制对于性能并不影响;当多线程的时候锁机制对于性能影响显著;

比例:(617+631):(336+348) = 1.824

   (892+936+935):(362+392+399)= 2.396

     (992 + 1077 + 1114 + 1173) : (416 + 420 + 427 + 436) = 2.563

 

  增量分析  线程增多单个线程运行时间增多: 线程一起分享cpu 时间,每个线程获得cpu独享时间减少,因此单个线程运行时间增多

       线程增多,加锁方法与平常方法时间比增加:等待线程越多,单个线程等待时间越长。但是为何增加加速度在减少,目前不清楚。

 

转载于:https://www.cnblogs.com/fantasy-es/p/4619077.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
synchronized和ReentrantLock的性能不能一概而论,因为它们在不同的场景下表现可能有所不同。早期版本的synchronized在很多场景下性能相差较大,但在后续版本中进行了较多改进,在低竞争场景中可能优于ReentrantLock。JDK6中synchronized入了自适应自旋、锁消除、锁粗化、轻量级锁、偏向锁等一系列优化,官方也提倡在能满足需求的前提下优先考虑使用synchronized进行同步。[1][2] ReentrantLock是标准的乐观锁的实现,它通过内部的while循环判断锁是否被其他线程所持有,当其他线程持有锁时,就一直自旋判断锁是否被释放。如果资源竞争激烈,同时锁竞争激烈,使用乐观锁可能导致很多线程一直在循环等待,当线程数和执行时间达到一个临界值时,乐观锁的性能可能比线程挂起的效率更低,循环等待的开销大于线程挂起的开销。因此,当需要锁的代码块执行时间普遍很长时,不建议使用ReentrantLock。[2] 另一方面,当资源竞争激烈,同时尝试获取锁的线程很多时,部分线程等待过久,如果使用synchronized导致锁慢慢膨胀,资源占有越来越多。为了保证synchronized性能锁的代码块需要保证执行时间稳定,不突然暴增。[2] 总的来说,synchronized和ReentrantLock在不同的场景下可能有不同的性能表现,需要根据具体情况选择合适的锁机制。synchronized在低竞争场景中可能优于ReentrantLock,而ReentrantLock则提供了更多的便利方法,可以进行精细的同步操作,甚至实现synchronized难以表达的用例。[1][3]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值