How bad is the new Thread (). Sleep versus Thread.sleep in terms of CPU and memory usage?

I know that sleep should be available in a static context. However, I need more resources so that I can protect this management. Most of the legacy codes that I'm processing now use the new Thread (). Sleep instead of Thread.sleep.

How bad is that?

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

compared to this?

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

EDIT:

    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");

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

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
+2
source share
6 answers

, . , Thread.sleep(). , , , . , , , - .

+9

: - , , ( , , ):

TimeUnit.SECONDS.sleep(5);
+4

? , .

, Thread.start(). Thread :

  • ,
  • ,
  • .

, , . ( , .)

/, ... , /... - / ThreadGroups ThreadLocals.

, , . new String(...) .


, . , , . . , . totalMemory() freeMemory() , GC.

+2

:

new Thread() , , ( , Thread.start(), ), , , , . .

1.3 , ... : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4229558 ThreadGroup threadCount ... , ThreaGroup (destroy()) , , . , destroy(). , ( ).

. , ThreadGroup , uncaughtException(Thread t, Throwable e) . ThreadGroup , . ( )

Java , . , ( , ).

+2

, Java , - sleep() . , , , , .

/ , , . , , , , .

+1

, .

new Thread().sleep(5000); , , . , Thread.sleep(5000);, .

, , , - , , ; .

, - :

Thread anotherThread = new Thread();

anotherThread.sleep(5000);

Thread.sleep(5000); . , .

, , . .

+1
source

All Articles