0

I am trying to understand the below line of difference between java wait() and sleep() methods. I understand basic of what each does but confused with the below statement frequently.

wait is called on Object while sleep is called on Thread.

what is object in above statement with respect to below code, because even thread object t is created like a any other object in java.

public class MyRunnableThreadClass implements Runnable{

public void run(){
try{
 Thread.sleep
}
catch(Exception e){}

}

public class ThreadDemo {

Thread t = new Thread ( new MyRunnableThreadClass()); 

}

Could you please help which is Object and thread in this cases

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
mahesh
  • 1,523
  • 4
  • 23
  • 39
  • 1
    `sleep` is a `static` method. `wait()` is an instance method declared in the class `Object`. – Sotirios Delimanolis Feb 24 '14 at 00:54
  • Typo. YOu got `Threed`instead of `Thread`. – Johannes H. Feb 24 '14 at 00:54
  • 1
    -This link may help: http://stackoverflow.com/questions/1036754/difference-between-wait-and-sleep? -A wait can be "woken up" by another process calling notify on the monitor which is being waited on whereas a sleep cannot. Also a wait (and notify) must happen in a block synchronized on the monitor object whereas sleep does not. – CKuharski Feb 24 '14 at 00:58

3 Answers3

1

wait is called on Object while sleep is called on Thread.

The statement isn't even correct.

  • wait() is a non-static method of Object, so must be called with a specific instance.

  • sleep() is a static method of Thread,, so it doesn't require a specific instance, but what it does is to put the current thread to sleep. It is called via the static invocation Thread.sleep(). Nothing to do with any specific object at all.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • thanks EJP, it means wait is not called on t in my context of code? and in this case on which instance i should use it here? – mahesh Feb 24 '14 at 01:06
  • If you don't call `wait()`, or call anything that calls it, which you aren't, it doesn't get called. Not knowing what your code is supposed to do, it is impossible to tell you where if anywhere you should call it. This is no place to ask for a `wait/notify` tutorial. – user207421 Feb 24 '14 at 02:32
0

Both methods make current thread to wait. So in both cases true object which is affected is current thread. In case of obj.wait(), current thread will be awaken when a notification is issued on that object, so obj can be considered as an additional parameter to the waiting procedure.

But if wait and sleep were declared as instance methods of class Thread, then they could be called on arbitrary thread, thus interfering execution of that other thread. This causes more harm than good, so it was prohibited by changing calling syntax and excluding explicit thread reference - thread.wait(obj) to obj.wait() and thread.sleep() to static Thread.sleep().

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
0

wait() is called on an object because it is a method of Object class. It helps in synchronization on object. It should be called with notify() when we release monitor.

Sleep is static method inside Thread class which put the current Thread in non-runnable state from running state.

Now after that sleep period, a Thread can wake up and able to come in a runnable state. It can then be scheduled by the scheduler to be in running state.

ikos23
  • 4,879
  • 10
  • 41
  • 60