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!

[353 byte] By [killer8685a] at [2007-11-27 11:49:53]
# 1

> I thank any help u gave!

Okay, so what's the problem you mentioned?

~

yawmarka at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 2

check this out it might help

http://www.java2s.com/Code/Java/Threads/Producer-Consumer.htm

mark07a at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 3

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?

killer8685a at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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?

~

yawmarka at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 5

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?

killer8685a at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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?

~

yawmarka at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 7

I know that wait,and notify can only be used in a synchronized method!

and thats all!There eny more?

killer8685a at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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?

~

yawmarka at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 9

Synchronize the 2 threads,i guess!Can u explain me?

killer8685a at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 10

> 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

~

yawmarka at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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.

jwentinga at 2007-7-29 18:27:56 > top of Java-index,Java Essentials,Java Programming...
# 12

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!

killer8685a at 2007-7-29 18:27:57 > top of Java-index,Java Essentials,Java Programming...