Need help with PRODUCER and CONSUMER
Hy to all!I have a problem with the producer and consumer aplication!
I need to make a producer which produces strings and put them at the end of a line.The consumer consumes the strings put by the producer from the beggining of the line!And i have to verify to make sure the line is not = 0,not empty!
I thank any help u gave!Thanks!
> I thank any help u gave!
Okay, so what's the problem you mentioned?
~
check this out it might help
http://www.java2s.com/Code/Java/Threads/Producer-Consumer.htm
My probleme is that i don't know how to make this application!I think i have to use pipes to put the strings in,but the consumer can't just consume the string because than the pipe will be empty!So i have to do something like the producer adds 2 ore 3 strings to the pipe and only after this should the consumer consume 1 string!Can you help me how to do it?
> Can you help me how to do it?
If by "help", you mean guide you so you can learn it yourself, sure. What have you researched so far about wait/notify?
~
I know that wait stops the consumer until the producer produce a string and the notify tells the consumer that he can consume a string!
am i right?
> I know that wait stops the consumer until the
> producer produce a string and the notify tells the
> consumer that he can consume a string!
> am i right?
Yes, it can be used that way. Do you know what synchronization is and how to use it?
~
I know that wait,and notify can only be used in a synchronized method!
and thats all!There eny more?
> I know that wait,and notify can only be used in a
> synchronized method!
> and thats all!There eny more?
Yes, do you know what the synchronized keyword does?
~
Synchronize the 2 threads,i guess!Can u explain me?
> Synchronize the 2 threads,i guess!
Yes, the term "synchronize" means "to synchronize", but that doesn't demonstrate a very good understanding of the concept. :o)
Go through the following in its entirety, and let me know when you understand:
http://java.sun.com/docs/books/tutorial/essential/concurrency/sync.html
~
> Synchronize the 2 threads,i guess!Can u explain me?
you do NOT do that.
Rather each thread will do its thing as and when it can, and wait if it can't until the other has done its thing.
So i read yhe articles u gave mi.i read that there are 2 tips of syncronization:synchronized methods and synchronized statements.
I know now that when one thread execute a synchronized method for one object,all other threads that uses that object will be blocked,until the first has finished with the object.when a synchronized method exits, he generates a happens-before relation,so that the changes made to this object will be visible to all other threads.
The synchronized statements are different.they need to speciife the intrinsic lock.The intrinsic lock. is like a semaphor,it coordinates the threads!