3

What is the difference between -

Thread workerThread = new Thread(runnable);
workerThread.start();
workerThread.sleep(5000);
.....

And

Thread workerThread = new Thread(runnable);
workerThread.start();
Thread.sleep(5000);

Do they both cause worker thread to pause sleep?

Thanks

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • possible duplicate of [Static method in Java can be accessed using object instance](http://stackoverflow.com/questions/4978000/static-method-in-java-can-be-accessed-using-object-instance) – nawfal Dec 16 '13 at 10:32

5 Answers5

8

No sleep is a static method that affects the current thread: http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#sleep(long)

So there is no difference, they both won't do what you want. The use of static methods on an instance is deprecated discouraged since it make you think you can do something you actually can't (like in this case)

Small addition - see John's answer to why calling static methods from class instances is allowed in Java :-)

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
  • +1: I am not sure it it deprecated in the sense that some methods are `@Deprecated`. However, it is highly discouraged as it doesn't do what it appears it should, as you say. – Peter Lawrey Apr 30 '11 at 16:44
  • @Peter - thanks. english is not my native language, so these comments are very very welcome :) – MByD Apr 30 '11 at 16:47
  • @MByD, English is my native language but corrections, clarifications and quibbles are always welcome. I can always improve. ;) – Peter Lawrey Apr 30 '11 at 16:57
  • "The use of static methods on an instance is deprecated discouraged since it make you think you can do something you actually can't (like in this case)" What do you mean, I can't pause the worker thread for 5 seconds ? – blue-sky Apr 30 '11 at 17:44
  • If you want to pause the worker thread, you will have to do that from within it. – MByD Apr 30 '11 at 18:10
  • Short note: using an IDE you can make it warn you if you call static methods on an instance (at least eclipse can) – Boris May 01 '11 at 09:12
4

sleep in both instances is the same static method that would cause the currently executing thread to sleep, not the workerThread.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
philwb
  • 3,805
  • 19
  • 20
3

The only actual call is

Thread.sleep(5000);

The other is an oddity of java that you can call static method's through instance variables.

You should always use

Thread.sleep(5000);

As it is better self-documenting that it is the current thread 'always' that is being slept on. If you do

myThread.sleep(5000);

You are not sleeping the myThread thread, you are sleeping the current thread.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
0

The second one should be preferred, because Thread.sleep is a static method, and should thus always be invoked, like all static methods, on the class defining it rather than on an instance of this class.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Here is another possibility. Use a volatile flag to pass the message to C.

volatile boolean startC = false;

void B() {
  // Do initial B work.
  startC = true;  // Set the volatile flag.
  // Finish B processing not relevant to C.
}

void C() {
  // Wait for B to progress far enough.
  while (!startC) {
    Thread.sleep(100);
  }
  // B has done enough work so we can begin.
}

This is not real code, just enough to give you the idea of what I am getting at. For example, you will need to pay attention to the visibility of the startC flag, or write a public getter and setter for it so B() and C() both have access.

rossum
  • 15,344
  • 1
  • 24
  • 38