Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads synchronization problem

 
Abhay Saini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have 2 questions.

Q1. now the definition of wait() function is like this : tells the calling thread to give up monitor and go to sleep until some other thread enters the same monitor and calls notify().
Now in this program two threads(One related to consumer and one to producer) use the same object q in get and put respectively. So is there only one monitor existing for the object q, which is entered when any thread uses a function involving q?

Q2 Here the Consumer waits until Producer notifies it, but the Producer also waits until the consumer notifies it? Who starts first?
In the output, Producer comes first but how?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
 
Abhay Saini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted this at Stackoverflow.com too
But i want to know as many opinions as possible.
Sorry if anyone is offended.
I will tell if I like any answer at that site so that your time is not wasted
 
Abhay Saini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the 2nd question
but not the first one?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Saini wrote:I posted this at Stackoverflow.com too
But i want to know as many opinions as possible.

No problem, but when you do that, please say that you've also posted it somewhere else and include a link, so that people don't have to waste time answering the question when it has already been answered somewhere else.
 
Dennis Grimbergen
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
No problem, but when you do that, please say that you've also posted it somewhere else and include a link, so that people don't have to waste time answering the question when it has already been answered somewhere else.

Well that is ofcourse what he wants. If you post a question at several places on the net then you get some responses. If 2 or more people give the same response while they respond to a message on a different forum, then these responses have some value.

I understand that for some more basic things this is not necessary and desirable, but if the question is more advanced these equal responses would give me a good feeling to look in such direction...
 
Haiti Meid
Greenhorn
Posts: 13
Chrome Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Please be forthright when crossposting to other sites.


I think that's his right. I got no problem with that.

@Abhay: Can you try this?



I renamed "sse" to "hasData" in order to make it more clear:
+ consumer waits for data.
+ producer puts data in, then notify(), and waits for consumer.
+ consumer handles the data, then notify().
+ procedure ends.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic