• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Producer and Consumer are separate classes, how to synchronize?

 
Tarek Khojah
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic