Need Help in Code Regarding Join (Threads Forum at JavaRanch)
Need Help in Code Regarding Join (Threads Forum at JavaRanch)
Java FAQ
Recent Topics
Register / Login
Author
ashwin bhawsar Ranch Hand Joined: Mar 16, 2011 Posts: 62
I like...
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.
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 .
www.coderanch.com/t/583455/threads/java/code-join
1/5
7/14/13
1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 . 9 3 . 9 4 . 9 5 . 9 6 . 9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 .
/ /T h r e a d _ B :I t e r a t et h eA r r a y L i s t c l a s sR e a d I n s t r u c t i o ni m p l e m e n t sR u n n a b l e{ s t a t i cL i s t < I n t e g e r >i n s t r u c t i o n s ; R e a d I n s t r u c t i o n ( L i s t < I n t e g e r >i n s t r u c t i o n L i s t ){ t h i s . i n s t r u c t i o n s=i n s t r u c t i o n L i s t ; } p u b l i cv o i dr u n ( ){ W r i t e T o F i l em=n e wW r i t e T o F i l e ( ) ; / / s t a r tt h et h r e a d _ C T h r e a dt h r e a d _ C=n e wT h r e a d ( m ," T h r e a d _ C " ) ; t h r e a d _ C . s t a r t ( ) ; w h i l e( t r u e ){ / /I t e r a t e f o r( i n ti=0 ;i<i n s t r u c t i o n s . s i z e ( ) ;i + + ){ i n ti n s t r u c t i o n=i n s t r u c t i o n s . g e t ( i ) ; / /S t o r ei ts ot h a tt h r e a d _ Cc a nr e a di t S t o r a g e C l a s s . s e t V a l u e ( i n s t r u c t i o n ) ; i n s t r u c t i o n s . r e m o v e ( i ) ; t r y{ / /i t e r a t et ot h en e x tv a l u eo fL i s to n l ya f t e rT h r e a d _ Ch a s / /f i n i s h e d / /i t sj o b . t h r e a d _ C . j o i n ( ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; } } } } } / /-T h r e a d _ Cw r i t e sc h a r a c t e r' # 't oF i l ec l a s sW r i t e T o F i l ei m p l e m e n t sR u n n a b l e{ B u f f e r e d W r i t e rb f ; W r i t e T o F i l e ( ){ t r y{ F i l ef=n e wF i l e ( " c : / m a c h i n e F i l e . t x t " ) ; F i l e W r i t e rf w=n e wF i l e W r i t e r ( f ) ; b f=n e wB u f f e r e d W r i t e r ( f w ) ; }c a t c h( I O E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; } } p u b l i cv o i dr u n ( ){ w h i l e ( t r u e ) { i f ( S t o r a g e C l a s s . g e t V a l u e ( ) > 0 ) { t r y{ b f . a p p e n d ( " L o o pc o u n t : "+S t o r a g e C l a s s . g e t V a l u e ( )+" \ n " ) ; f o r( i n ti=S t o r a g e C l a s s . g e t V a l u e ( ) ;i>0 ;i ){ b f . a p p e n d ( " # " ) ; } T h r e a d . s l e e p ( 5 0 0 0 ) ;/ / S l e e pf o rs o m et i m e . b f . a p p e n d ( " \ n " ) ; b f . f l u s h ( ) ; S y s t e m . o u t . p r i n t l n ( " W r i t t e n : "+S t o r a g e C l a s s . g e t V a l u e ( ) ) ; S t o r a g e C l a s s . s e t V a l u e ( 0 ) ; }c a t c h( I O E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ;
www.coderanch.com/t/583455/threads/java/code-join
2/5
7/14/13
1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 . 1 1 4 . 1 1 5 . 1 1 6 . 1 1 7 . 1 1 8 . 1 1 9 . 1 2 0 . 1 2 1 . 1 2 2 . 1 2 3 . 1 2 4 . 1 2 5 . 1 2 6 . 1 2 7 . 1 2 8 . 1 2 9 . 1 3 0 . 1 3 1 . 1 3 2 . 1 3 3 . 1 3 4 . 1 3 5 . 1 3 6 . 1 3 7 . 1 3 8 . 1 3 9 . 1 4 0 . 1 4 1 . 1 4 2 . 1 4 3 . 1 4 4 . 1 4 5 . 1 4 6 . 1 4 7 .
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.
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)
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 .
www.coderanch.com/t/583455/threads/java/code-join
3/5
7/14/13
0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . j e c t 7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 .
www.coderanch.com/t/583455/threads/java/code-join
4/5
7/14/13
9 2 . 9 3 . 9 4 . 9 5 . 9 6 . 9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 .
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads JVM and Thread Help Thread problem proble in converting ArrayList<String> to ArrayList<Object> cannot make static reference to non-static Integer [ A good finding ] - User & Deamon Threads
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:27:25 .
www.coderanch.com/t/583455/threads/java/code-join
5/5