Jyotirup
Jyotirup

Reputation: 2920

Calling wait() on Thread instance in java

What will happen if we call wait() method on instance of Thread class.

Thread t1 = new MyThread();
t1.wait();

What will be the state of my thread t1?

Upvotes: 0

Views: 3147

Answers (7)

Anonemous
Anonemous

Reputation: 309

Please refer java doc from java.lang.Thread#join(long):

This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

If you use wait on Thread instances, then As a thread terminates the this.notifyAll method is invoked. you will get confusing behavior which is hard to debug.

Upvotes: 1

Voo
Voo

Reputation: 30206

To add something that has so far not been said: Actually something "out of the ordinary" will or at least can happen when you call wait on a thread object.

That is because Thread internally waits on this internally and obviously that leads to interesting results to say the least.

Upvotes: 1

Gray
Gray

Reputation: 116848

As @Tudor (and others) have mentioned, you cannot call wait() on an object that you aren't synchronized on so you would get an IllegalMonitorStateException. The thread will not be affected.

Thread t1 = new MyThread();
// this will throw
t1.wait();

You most likely should be calling t1.join(). This waits for the thread to finish before continuing. However, you have not started your Thread t1 so the join would wait forever. If the thread had already been started, then when the Thread.run() method finishes (either because of it returns or throws an exception), the join() will return.

Thread t1 = new MyThread();
t1.start();
// this waits for t1 to finish
t1.join();

Upvotes: 3

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

There is no Thread#wait method. wait is inherited from Object and is used for synchronization. So unless you have synchronized(t1) first, you will just get an illegal monitor exception.

t1.wait will NOT WAIT FOR THE THREAD TO FINISH. It waits for a lock put on the thread object by another thread.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691635

It will be the same as before your call to wait(). wait() is a method of java.lang.Object. It waits on the intrinsic monitor associated to the object. In that case, the object is a thread, and that doesn't change what wait() does. It's pretty bizarre to use a Thread object as a monitor, though. You should use a private and final object as monitor.

Upvotes: 7

esaj
esaj

Reputation: 16035

It's still the java.lang.Object's wait you're calling, so only the current thread will wait (assuming you're holding the objects monitor, otherwise an IllegalMonitorStateException will be thrown).

Upvotes: 1

Tudor
Tudor

Reputation: 62439

Nothing out of the ordinary will happen. You will just suspend the current thread until some other thread calls t1.notify. You are simply using t1 as a plain monitor.

Btw, your code will throw an IllegalMonitorStateException because you are calling wait outside a synchronized block.

Indeed I find java's choice of methods a bit annoying, especially regarding the thread class.

Take for instance this scenario:

Thread t = new Thread();
t.start();
t.sleep(1000);

Which thread will sleep? The current one "obviously".

Upvotes: 2

Related Questions