File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes notify() problem in Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "notify() problem in Thread" Watch "notify() problem in Thread" New topic
Author

notify() problem in Thread

sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

I wrote a code for stack in which first three item will be pushed then last item will be poped using two threads with same object
e.g.
if we push 1 2 3 4 5 6 7 8 9
then 1,2 and 3 will be inserted and 3 will be poped out then 6 and then 9.
here is the code


the problem is the notify() is not working properly. Please help.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi Sonu,

What do you mean by
notify() is not working properly


Please, TellTheDetails.

Also, I think you'd like to go through this thread.

I hope this helps.

P.S. I've aligned the code properly - to make it more readable.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43



What do you mean by
notify() is not working properly



I am taking lock on class using two threads pop and push but while push is on wait() pop execute but it don't notify() push after its execution and that results in situation like death-lock push is still waiting for pop to notify but the notify() in pop thread is not working.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Put "try{c.wait();}catch(Exception e){} " outside the for loop.
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

Gaurangkumar Khalasi wrote:Put "try{c.wait();}catch(Exception e){} " outside the for loop.


Not working!!

Still the first thread is waiting for notification but notify is not working!!
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Again, please, TellTheDetails.
By that, I mean:
1) Tell us what is expected behavior according to you
2) Tell us why do you think that should be the behavior (and how it is related with code)
3) Tell us what is exactly happening currently (a little more elaboration than 'notify() not working')

Also, (again)please, go through the thread I've mentioned in my previous post. It might help you.

Even after this, if you have any doubts, feel free to post here.

I hope this helps.
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

this is the code:


And this is the output

https://www.drivehq.com/file/DFPublishFile.aspx/FileID1103443199/Keyjkrwixosduyn/Output.jpg

You can see in the above image(actual output)
first thread waits(Push Thread) and second thread(Pop Thread) starts and the second thread notify and waits then first thread waits and it keep on waiting...
I want the these two threads to wait and notify alternatively one by one (expected output).

so I want this output
1
2
3
Push waiting
3
Notifying Push
Pop Waiting
4
5
6
Notifying Pop
Push Waiting
6
Notifying Push
Pop Waiting
7
8
9
Notifying Push
Pop Waiting
Notifying Pop

Hope this time the question is clear
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

sonu raj wrote: I want the these two threads to wait and notify alternatively one by one (expected output).

So, this is the problem.

You can check threading chapter of any descent Java book regarding this - e.g. how to tackle issues like T1 notifies T2 and then T2 enters in wait state, and so on.

I hope this helps.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

sonu raj wrote:this is the code:


And this is the output

https://www.drivehq.com/file/DFPublishFile.aspx/FileID1103443199/Keyjkrwixosduyn/Output.jpg

You can see in the above image(actual output)
first thread waits(Push Thread) and second thread(Pop Thread) starts and the second thread notify and waits then first thread waits and it keep on waiting...
I want the these two threads to wait and notify alternatively one by one (expected output).

so I want this output
1
2
3
Push waiting
3
Notifying Push
Pop Waiting

4
5
6
Notifying Pop
Push Waiting
6
Notifying Push
Pop Waiting

7
8
9
Notifying Push
Pop Waiting

Notifying Pop

Hope this time the question is clear



There is no loop anywhere in your showpop() code, so why did you expect it to print its output more than once?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

Henry Wong wrote:

There is no loop anywhere in your showpop() code, so why did you expect it to print its output more than once?

Henry


I am Sorry!! that was a silly mistake
corrected my code now its working properly:



Once again Thank You Henry!!
 
Consider Paul's rocket mass heater.
 
subject: notify() problem in Thread