File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Buffering using Thread Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Buffering using Thread Synchronization" Watch "Buffering using Thread Synchronization" New topic

Buffering using Thread Synchronization

John Turner

Joined: Dec 19, 2010
Posts: 6
Hi all,

I am having a little trouble with my buffer that uses Thread synchronization. What is meant to happen is that the InputManager inputs a String into the buffer. The buffer is then passed to LineToChar, where it takes the String from the buffer using the take() method and extracts the string into individual letters. These individual letters should then be passed to the OutputManager through another buffer, where the OutputManager outputs the individual letters onto the screen.

The problem is that it appears to be getting stuck in deadlock after the letter "e". Can you please offer suggestions on why it is doing this and point me in the right direction on how to solve this? My code is below:

Hope you can help me.
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

I don't see the code for LineToChar. Can you show that please?

John Turner

Joined: Dec 19, 2010
Posts: 6
Sorry, I forgot about that! I was tired when posting last night. Here is

Sergey Babkin
Ranch Hand

Joined: Apr 05, 2010
Posts: 50
You never start the Output Manager threads. In LineToChar for each input line you create a new OutputManager on the same bufferOut but never start it. So nothing consumes the lines from bufferOut. Creating a thread object doesn't start the thread. And you probably only need to call and start one OutputManager object, not create them every time.

Also, I think there an error in InputManager: to start a thread, use the method start(), do not call directly run(). Run() is just a method like any other, and if you call it directly, it will be executed in your existing thread. Start() on the other hand creates a new thread and then executes the run() method there. So the only special thing about run() is the name, in that start() knows about it.
I agree. Here's the link:
subject: Buffering using Thread Synchronization
jQuery in Action, 3rd edition