• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Buffering using Thread Synchronization

 
John Turner
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see the code for LineToChar. Can you show that please?
 
John Turner
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I forgot about that! I was tired when posting last night. Here is LineToChar.java

 
Sergey Babkin
author
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic