File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notify() problem in Thread

 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put "try{c.wait();}catch(Exception e){} " outside the for loop.
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
Android C++ Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20826
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic