• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in code regarding join()

 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently iam learning Threads topic from Kathy Sierra.
I am trying to write a typical producer-consumer code that makes use of the join() method of Thread.

This is what i want to accomplish :
1> There will be 3 threads Thread_A, Thread_B and Thread_C.

2> Thread_A's job is to continuously take input from the User from console and store it in an ArrayList.
The input will be in form of positive integer numbers ( eg: 2, 3, 100, 50 ....).

3> Thraed_B's job is to iterate through the array list and read these values one-by-one and store it in a variable.
Thraed_B will not read the next values unless Thread_C has finished its job.( Thread_B has join() on Thread_C)

4> Thread_C's job is to print '#' character in a File as per the values in the variable. So if the
values is say 20, then Thread_C will print '#' 20 times in the file.

To summarize, if the user has entered (20, 5, 10, 50) then Thread_C should print '#' 20 times, then 5 times then 10 times.....

I am not getting where to start the Thread_C.


 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to keep thread_C running, but doing so thread_B will never move forward as thread_C's run is in while loop, so it will never die.
 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The approach was wrong. The solution to the problem can be achieved using the wait() and notify() methods.

I had put an infinite loop in Thread_C to continually monitor if the Thread_B has written some value, but doing so Thread_C was not exiting the run method and so not going "dead".
As Thread_B had join over Thread_C, Thread _B could not go further unless Thread_C was not over and this was not happening.

To achieve the goal i have used the wait() and notify() methods.
Below is a new code that will make clear how to achieve the goal using wait() and notify() methods.
( Note : The code now does not take any input from the user, instead the producer produce the values)




 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic