my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Why thread keeps waiting even after notification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why thread keeps waiting even after notification" Watch "Why thread keeps waiting even after notification" New topic
Author

Why thread keeps waiting even after notification

ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

I have a Counter Class with two methods : increaseCount() and decreaseCount().
I also have two threads. Thread_1's run calls the increaseCount() and Thread_2's run calls the decreaseCount().
Both threads share a common instance of Counter class.

Now, what i want is Thread_2 to execute decreaseCount() only after Thread_1 has finished executing the increaseCount().
( i know this can be done with join() method, but want to achieve the same thing using wait() and notify() )

I have made the Thread_2 to wait till the Thread_1 completes its execution. After Thread_1 finishes its execution it call the notify() on the shared object.
But the problem is that Thread_2 keeps on waiting even after Thread_1 notifies it. Why ? am i missing something ?


Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

ashwin bhawsar wrote:I have made the Thread_2 to wait till the Thread_1 completes its execution. After Thread_1 finishes its execution it call the notify() on the shared object.
But the problem is that Thread_2 keeps on waiting even after Thread_1 notifies it. Why ? am i missing something ?

No offenses, but your code is pretty classic example of improper usage of wait/notify mechanism.

Hint : Thread 2 starts waiting on object obj after Thread 1 has invoked notify over object obj. Who will wake up Thread 2?

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

Ok, so you are saying that Thread_1 completes its execution, calls notify() and after this Thread_2 starts the execution and waits and has no one to notify it.

But what is i make Thread_1 sleep for some time and allow Thread_2 to execute ?
I tried this but still doesn't works.

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

ashwin bhawsar wrote:But what is i make Thread_1 sleep for some time and allow Thread_2 to execute ?
I tried this but still doesn't works.


Please remember that you are making a sleep call inside synchronized block. Since both threads are synchronized on same object, there's no way that other thread would enter in synchronized block and start waiting.

I hope you get what is the problem.

This is a typical problem, which fortunately has typical solution. You should be able to find it in threading chapter of any descent book.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

I hope this helps.
ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

Ok, got what the problem is.

One way to solve this problem is to swap thread calling

So now this is what will happen :
Thread_2 will start first -> enter sync method 'decreaseCount()' -> go in a wait state and wait for someone to notify -> release the lock for other thread.
Thread_1 will start -> enter the sync method 'increaseCouint()' -> acquire the lock -> complete the execution -> call the notify() -> release the lock.
Thread_2 which is waiting now will be notified -> it will execute the remaining code.


Thanks for your time.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

You are welcome, but I'm afraid that the problem is not solved.

You should never, never, never rely upon particular ordering during multi-threaded program. Just because you say t1.start(); t2.start(); it doesn't guarantee that t1 will start before t2.

Just switching the order won't help. The problematic scenario can still happen.

Firstly, you'll have to accept the fact that since there are two threads, any thread can execute in any order.

Secondly, I will (again) suggest to take a look at any threading chapter of any good book. Its good that you've understood the problem, but there is a solution to this. And understanding that solution is equally important.
Ashok Iraveni
Greenhorn

Joined: Jul 27, 2012
Posts: 2
ashwin bhawsar wrote:I have a Counter Class with two methods : increaseCount() and decreaseCount().
I also have two threads. Thread_1's run calls the increaseCount() and Thread_2's run calls the decreaseCount().
Both threads share a common instance of Counter class.

Now, what i want is Thread_2 to execute decreaseCount() only after Thread_1 has finished executing the increaseCount().
( i know this can be done with join() method, but want to achieve the same thing using wait() and notify() )

I have made the Thread_2 to wait till the Thread_1 completes its execution. After Thread_1 finishes its execution it call the notify() on the shared object.
But the problem is that Thread_2 keeps on waiting even after Thread_1 notifies it. Why ? am i missing something ?






Hi Ashwin

Use one boolean flag in COunter class,

like

boolean hasIncreased = false ;

and check this flag before waiting in Decrement logic instead waiting all the time.

thanks
spar mc
Greenhorn

Joined: Jul 30, 2012
Posts: 5
You cannot guarantee the order of thread's execution when the run in parallel, CPU gives its time to each thread as directed by Operating System, you cannot control that.

The good option is already mentioned in a reply, use a boolean flag.

Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Have a look at this Tutorial . A Producer -Consumer problem handled using wait and notify features.
gihan dissanayaka
Greenhorn

Joined: Sep 03, 2011
Posts: 8
My opinion is only you have to change the order of starting threads to get what you want
t2.start();
t1.start();

if you are using notify() that means you are calling to thrads which are waiting.
if you start thread 1 first what happens when it meets the this.notify()???
it is notifying to waiting threads.but there's none
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why thread keeps waiting even after notification