就CPU和内存利用率而言,新Thread().sleep与Thread.sleep相比有多糟糕? [英] How bad is new Thread().sleep compared to Thread.sleep in terms of CPU and memory utilization?

查看:133
本文介绍了就CPU和内存利用率而言,新Thread().sleep与Thread.sleep相比有多糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道应该在静态上下文中访问睡眠.但是,我需要更多的投入,因此我可以为管理层辩护.我正在处理的大多数旧代码现在都使用new Thread().sleep而不是Thread.sleep.

I'm aware that sleep should be accessed in a static context. However I need more inputs so I can defend this to the management. Most of the legacy code that I'm handling now uses new Thread().sleep instead of Thread.sleep.

这有多糟?

for (int c = 0; c < 5; c++) {
    new Thread().sleep(5000);
}

相比于此?

for (int c = 0; c < 5; c++) {
    Thread.sleep(5000);
}

    final long start = System.currentTimeMillis();
    System.out.println("Total memory: " + Runtime.getRuntime().totalMemory());
    System.out.println("Free memory: " + Runtime.getRuntime().freeMemory());
    System.out.println("===========================");
    for (int c = 0; c < 5; c++) {
        new Thread().sleep(5000);
        System.out.println("Used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
        System.out.println("===========================");
    }
    System.out.println("Total memory: " + Runtime.getRuntime().totalMemory());
    System.out.println("Free memory: " + Runtime.getRuntime().freeMemory());
    System.out.println("===========================");
    System.out.println("Time elapsed: " + (System.currentTimeMillis() - start) + " milliseconds");

结果(新Thread().sleep):

Result (new Thread().sleep):

Total memory: 5177344
Free memory: 4990904
===========================
Used memory: 186440
===========================
Used memory: 205136
===========================
Used memory: 205136
===========================
Used memory: 205136
===========================
Used memory: 205136
===========================
Total memory: 5177344
Free memory: 4972208
===========================
Time elapsed: 24938 milliseconds

结果(Thread.sleep):

Result (Thread.sleep):

Total memory: 5177344
Free memory: 4990904
===========================
Used memory: 186440
===========================
Used memory: 186440
===========================
Used memory: 204848
===========================
Used memory: 204848
===========================
Used memory: 204848
===========================
Total memory: 5177344
Free memory: 4972496
===========================
Time elapsed: 24938 milliseconds

推荐答案

这只是文盲的编程,就这么简单.同样适用于以这种方式调用任何静态方法,它不限于Thread.sleep().它强加了无意义的空间和时间成本,但更糟的是,这背叛了程序员的主要概念误解.我不会花很多精力去解决所有问题,但是我肯定会在对相关人员进行再教育方面做些事情.

It is just illiterate programming, simple as that. Same applies to calling any static method that way, it's not confined to Thread.sleep(). It imposes pointless space and time costs, but worse it betrays a major conceptual misunderstanding on the part of the programmer. I wouldn't devote much energy to going back and fixing all the occurrences but I would certainly do something about re-educating the personnel concerned.

这篇关于就CPU和内存利用率而言,新Thread().sleep与Thread.sleep相比有多糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆