This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Producer and Consumer are separate classes, how to synchronize? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Producer and Consumer are separate classes, how to synchronize?" Watch "Producer and Consumer are separate classes, how to synchronize?" New topic
Author

Producer and Consumer are separate classes, how to synchronize?

Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
Well, first off, quick question:
Monitors, are they per object or per reference? I'm assuming per object. But I wrote some code with that in mind and it didn't seem to work.

Anyway, here's my problem:

Two classes, each has an inner class implementing runnable that does the following:
One captures audio and writes to a piped output stream. (Operation "A")
One reads from the piped input stream and play the audio. (Operating "B")

The two classes are not related at all, except that one of them uses a getter method for the piped stream so that I can connect them.

Now, if I just start the threads, there's no real fairness, capture more than I play or play more than I capture.

I need to make it one-to-one, A/B/A/B/A/B/A/B in perfect order.

I know (somewhat) how to use wait() and notify(), but again, the classes have nothing in common, I tried synchronizing on the piped stream I wrote a getter method for but that didn't seem to work for some reason.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Well, first off, quick question:
Monitors, are they per object or per reference? I'm assuming per object. But I wrote some code with that in mind and it didn't seem to work.


You are correct. Synchronization are based on objects -- not references. Please show the code that you wrote, and explain what you mean by "didn't seem to work".

Henry
[ December 01, 2007: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Anyway, here's my problem:

Two classes, each has an inner class implementing runnable that does the following:
One captures audio and writes to a piped output stream. (Operation "A")
One reads from the piped input stream and play the audio. (Operating "B")

The two classes are not related at all, except that one of them uses a getter method for the piped stream so that I can connect them.

Now, if I just start the threads, there's no real fairness, capture more than I play or play more than I capture.

I need to make it one-to-one, A/B/A/B/A/B/A/B in perfect order.

I know (somewhat) how to use wait() and notify(), but again, the classes have nothing in common, I tried synchronizing on the piped stream I wrote a getter method for but that didn't seem to work for some reason.


If you are willing to change your code a bit, you may want to consider a java.util.concurrent.LinkedBlockingQueue. Have your producer write to it, and have your consumer read from it.

If you set the capacity to one, then the producer will always be exactly one ahead of the consumer -- giving you that alternating pattern.


BTW, comments like "didn't seem to work for some reason" doesn't really help -- we need to see code and a description to be able to help.

Henry
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

If you are using a piped input/output stream pair then everything should be in place for you. I am not sure why you want that pattern to occur. What is the problem if you write more and the reader is reading at its own leisure. This infact is what we call buffering the audio stream which in most of the cases is required.
You really do not need to synchronize if you are using piped input/output stream.
Let know if i am missing something.


apigee, a better way to API!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Producer and Consumer are separate classes, how to synchronize?
 
Similar Threads
Question for you developers out there
Playback of RTP Audio/Video Streams.
HFS - Compression Filter - has anyone else tried this ?
InputStream.read(byte[])
How to keep a stream open for multiple classes to access