Producer consumer program in java using threads


















Also I noticed that the Consumer does not need the size of the Queue. This is a dead variable. Nice work on your blog, btw. Keep it up!!!! I believe there is a race condition in the "produce " method. If two threads were blocked because the queue is full, then a consumer's call to notifyAll would unblock both of them, and they'd both add to the queue. I think in this program there is just one producer, but even in case of multiple producer, since adding to queue is happening inside synchornized block, only one thread will add value at a time, but I agree the code has bug.

Size checking of queue should be inside first synchronized block, there is no need for one more synchronized block in this example. You should try the example that is given here. Hey can't we have the below code in the same synchronized block, the block in which we had written sharedQueue. Very nice implementation i got full clarity Thank u so much Javin Paul. Please correct me if I am wrong. In the consumer part. I believe its better to notify the other threads only after it is consumed.

Kiran, yes, that would be better, but it doesn't affect the output of program because both statement are inside synchronized block and lock will be release only when thread goes output synchronized block, so even thread will get notification, they won't check again until you remove the element and release the lock. Having said that, I agree your solution is cleaner,. Please help me understand why we need to synchronize sharedQueue, when Vector is already thread safe.

Hello Punit, you need to synchronize sharedQueue because you are using it for inter-thread communication i. Vector is thread-safe for it's own operation e. Those are done by using wait and notify and there you need a common object to synchronize it. Since sharedQueue is common between Producer and Consumer we synchronize on that.

Vector itself is synchronized. Why do we need synchronized block for sharedQueue? Object, transfer the milk box object as a structural method parameter, because in this class, you want to call the operation of getting milk. Create 2 thread objects, respectively transmit producers and consumer objects as constructor parameters. The SDK1. Code: result: Loc Review the producer consumer code learned in the previous section: With two threads: It is better to solve the function of two threads, one producer and one consumer alternately, through the if judgme Requirements: Two threads simulate the ticket, the ticketing process.

However, if each ticket is required, a ticket is sold, after sell, then deposit until the end of the sale. Use of knowledge points Problem Description: Producer and consumer issues are classic problems in thread models: producers and consumers share the same storage space in the same time, producers to store spaces, consumers tak Implementation through WAIT and Notify in Synchronize [1] We can write the methods of producers and consumers in public category [2] Call the producer and consumer method in the main function, and Producer and consumers Code requirements know what to do Communication issues between threads and support of Object classes Basic model It is now desirable to ac My answer is the same as the previous question: read about Java concurrency.

Spend an hour reading about synchronization, locks, and atomic variables and I guarantee you will easily write your program. For producer consumer problem best solution is BlockingQueue.

I was testing a few things so designed same kind of program now modified it as per your need. I tried the following which might work for you, except for the buffer condition on 3, which you can add the part of the code by yourself. Hope this helps. Please refer the below code. You can change the constant values based on the command line arguments. I have tested the code, its working as per your requirement. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Producer Consumer using threads Ask Question. Asked 8 years, 2 months ago. Active 2 years, 7 months ago. Viewed 10k times. Below is the Java code I wrote. Improve this question. Prev Next. More topics on Java Programming. Email We respect our user's data, your email will remain confidential with us. Subscribe to Our Newsletter.



0コメント

  • 1000 / 1000