wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes threads synchronization problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "threads synchronization problem" Watch "threads synchronization problem" New topic
Author

threads synchronization problem

Abhay Saini
Greenhorn

Joined: Jun 15, 2011
Posts: 19

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

Please be forthright when crossposting to other sites.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abhay Saini
Greenhorn

Joined: Jun 15, 2011
Posts: 19
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

Joined: Jun 15, 2011
Posts: 19
I got the 2nd question
but not the first one?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Nov 04, 2009
Posts: 140

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...


SCJP, SCWCD, SCJD
Haiti Meid
Greenhorn

Joined: Jun 08, 2011
Posts: 13

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.

Sorry for my English...
 
Consider Paul's rocket mass heater.
 
subject: threads synchronization problem
 
Similar Threads
producer/consumer problem
Wait , notify() ...explain this output
thread's
Explain me this output (notify)
Concurrency Utilities