Академический Документы
Профессиональный Документы
Культура Документы
Java FAQ
Recent Topics
Register / Login
A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal
Author
code:class Producer implements Runnable{ SyncStack stack; public Producer(SyncStack s){ stack = s; } public void run(){ for(int i=0; i<20; i++){ char c =(char)(Math.random()*26+'A'); stack.push(c); System.out.println(" "+c); try{ Thread.sleep((int)(Math.random()*100)); }catch(InterruptedException e){} } } } class Consumer implements Runnable{ SyncStack stack; public Consumer(SyncStack s){ stack = s;
www.coderanch.com/t/233920/threads/java/producer-consumer
1/5
6/13/13
} public void run(){ for(int i=0;i<20;i++){ char c = stack.pop(); System.out.println(" : "+c); try{ Thread.sleep((int)(Math.random()*1000)); }catch(InterruptedException e){} } } }
class SyncStack{ private int index = 0; private char []data = new char[6]; public synchronized void push(char c){ while(index == data.length){ try{ this.wait(); }catch(InterruptedException e){} } this.notify(); data[index] = c; index++; } public synchronized char pop(){ while(index ==0){ try{ this.wait(); }catch(InterruptedException e){} } this.notify(); index--; return data[index]; } } public class SyncTest{ public static void main(String args[]){ SyncStack stack = new SyncStack(); Runnable p=new Producer(stack); Runnable c = new Consumer(stack); Thread t1 = new Thread(p); Thread t2 = new Thread(c); t1.start(); t2.start(); } }
6/13/13
i want to know If the implementation of this sentence "while(index == data.length){try{ this.wait(); }catch(InterruptedException e){} }"
Hi lianchi gao, First things first, while posting code please put it within the code tags. Various tags can be inserted in the messages using the buttons available at the bottom of the editor. So, you have written this code yourself of you are trying to check out some one else's code? Anyways, the code has the following problems: You must use notifyAll and not notify You must call notify only after putting/removing the char in the data array No point in sleeping in producers and consumers since the push and pop are blocked till they do their work Condition index == data.length is redundant as it is always true Push and pop must not be in a while loop but just guarded by an if condition
This code may look like the following: DISCALIMER: I have just changed the code you have posted to meet the requirements and this in no way is the perfect producer-consumer example.
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 .
www.coderanch.com/t/233920/threads/java/producer-consumer
3/5
6/13/13
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 .
} } } c l a s sC o n s u m e ri m p l e m e n t sR u n n a b l e{ S y n c S t a c ks t a c k ; p u b l i cC o n s u m e r ( S y n c S t a c ks ){ s t a c k=s ; } p u b l i cv o i dr u n ( ){ w h i l e ( t r u e ){ c h a rc h a r a c t e r=s t a c k . p o p ( ) ; S y s t e m . o u t . p r i n t l n ( " R e c e i v e dc h a r a c t e r :"+c h a r a c t e r ) ; } } } c l a s sS y n c S t a c k{ p r i v a t ei n ti n d e x=0 ; p r i v a t ec h a r [ ]d a t a=n e wc h a r [ 6 ] ; p u b l i cs y n c h r o n i z e dv o i dp u s h ( c h a rc ){ i f( i n d e x= =6 ){ t r y{ t h i s . w a i t ( ) ; }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 ){ } } d a t a [ i n d e x ]=c ; i n d e x + + ; t h i s . n o t i f y A l l ( ) ; } p u b l i cs y n c h r o n i z e dc h a rp o p ( ){ i f( i n d e x= =0 ){ t r y{ t h i s . w a i t ( ) ; }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 ){ } } i n d e x ; t h i s . n o t i f y A l l ( ) ; r e t u r nd a t a [ i n d e x ] ; } } p u b l i cc l a s sS y n c T e s t{ p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ){ S y n c S t a c ks t a c k=n e wS y n c S t a c k ( ) ; R u n n a b l ep=n e wP r o d u c e r ( s t a c k ) ; R u n n a b l ec=n e wC o n s u m e r ( s t a c k ) ; T h r e a dt 1=n e wT h r e a d ( p ) ; T h r e a dt 2=n e wT h r e a d ( c ) ; t 1 . s t a r t ( ) ; t 2 . s t a r t ( ) ; } }
www.coderanch.com/t/233920/threads/java/producer-consumer
Answering your question, yeah that piece of code gets executed in two cases: When the condition guarding the wait is not satisified
4/5
6/13/13
When the producer/consumer is waiting and it gets a notification call and the while loop condition is false
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
Similar Threads Question on Semaphores Important, about threads Thread wait() and notify() wait and notify producer consumer thread with implementation of concurrent linked queue
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 12, 2013 22:22:09 .
www.coderanch.com/t/233920/threads/java/producer-consumer
5/5