Комментарии:
Important: The last part about using wait-notify is incorrect. My mistake, sorry about that.
The object used to wait/notify should be the same object used by threads to synchronize. So correct code should be similar to:
synchronize(sharedQ){
sharedQ.wait();
}
synchronize(sharedQ){
sharedQ.notifyAll();
}
That was a basic mistake which I should have caught. I feel bad about misleading the initial viewers. Sorry.
It would be more helpful if you provide the sample code or example code link. Thanks for such a nice video!
ОтветитьHi @Defog
Why there is no while condition on put method
It will have the same problem in adding the item in max index position
Pls correct me if iam wrong
Greetings from Russia , having watched hours of content on concurrency/multithreading , this is the by far the best one i have seen , top notch.
Ответитьfirst time learning about producer-consumer. your explanation is very clear and easy to understand
ОтветитьJust AMAZING.
ОтветитьI love your tutorials! I did this example myself and found a little but meaningful bug:
-notEmpty and notFull should be called ".signal()" instead of ".signalAll()" because in case of releasing one spot in the queue by take() method, if there are multiple "putting" Threads, they are all notified at once about free space and they add concurently its own item exceeding the maxSize (and later condition `queue.size() == max` doesn't catch situation when this size is exceeded)
I still don't understand how can two consumer threads wait on the condition because we have been using a lock over that method in the first place.
ОтветитьnotEmpty and notFull not needed ..we can just use this.wait()... this implementation throw java.lang.IllegalMonitorStateException: current thread not owner... i tried it..
ОтветитьWhy does java releases the lock when lock state is false. does this allow multiple threads to come in and wait? Because of this we need to solve the problem where multiple consumers trying to read the same item.
ОтветитьHi Mate; error correction: you should use notEmpty.signal() and not notEmpty.signalAll()
ОтветитьI don't understand if there was a lock, how did two threads enter the method?
ОтветитьVery well explained thank you !
ОтветитьIf we are taking same lock in both put and take method, then wouldn't it be that only one thread will do either put or take.
If a thread is in put, no thread can go in take. And vice versa.
Is that possible?
Clear explanation!!
ОтветитьThanks
ОтветитьBest clear explanation of a very complicated subject
ОтветитьA perfect explanation I had ever seen. Thanks a lot!
ОтветитьBro you are best guru out there . Put more contents your channel will bloom
ОтветитьBro, you rock. tks a lot!
ОтветитьI have similar problem where two threads need to read element by element.. i tried same concept but will not work. Two threads started but not moving further
ОтветитьNeat and clear explanation...
Ответить@9.04, how two thread could come at that point since we called lock above. I think only one thread will be in await and when the notEmpty.signalall is called that thread will remove the item and call the unlock so the other thread can come inside and do the if check and can go to the await state. What is the need to while there?
ОтветитьSince a long time no video, as you have also switched to new job, pls start creating new videos
ОтветитьI am getting illegalMonitorstateException. Any idea when it occurs? I followed the same approach.
Ответитьcould have been better if the live coding was done for the example
ОтветитьAnother Gem of a video from you on java Concurrency. I've a question though, How does changing if condition to while would solve the problem ? Can you please elaborate ?
Ответитьcan anyone please help me with this code
it is throwing illegal monitor state exception
class CustomBlockingQueue{
Queue<Integer> q=new PriorityQueue<>();
Lock lock= new ReentrantLock();
int max=10;
Condition full=lock.newCondition();
Condition empty=lock.newCondition();
void add() throws InterruptedException {
while (true){
lock.lock();
try{
if(q.size()==max){
empty.await();
}
q.add(1);
full.signalAll();
System.out.println("adding element");
}finally{
lock.unlock();
}
}
}
void remove() throws InterruptedException {
while (true){
lock.lock();
System.out.println("remove aqquireing lock");
try{
System.out.println(q.size());
if(q.size()==0){
System.out.println("waiting");
full.wait();
}
q.remove();
empty.signalAll();
System.out.println("removing element");
}finally{
lock.unlock();
}
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
CustomBlockingQueue customBlockingQueue=new CustomBlockingQueue();
new Thread(()-> {
try {
customBlockingQueue.add();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
customBlockingQueue.remove();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
Great videos. it makes it easy to understand complex concepts. Though I have confused about using the while loop instead of the If condition. It could have been better to explain issues with if condition here and then introduce a while loop. anyway amazing explanation with exaples
ОтветитьHi Deepak, Please start posting new videos. Your way of explaining the things in simple language helps to understand the concepts better. Great job! Looking forward to learn from your new videos
ОтветитьGreat explanation on producer and consumer using different ways. It makes the idea really clear. I was wondering if there is any way to handle the fairness for consumers meaning if any consumer goes to wait-state first he will be notified first.
Ответитьthis video has cleared a lot of my doubts and the way you have explained will help me remembering it for a very very long time ! Thanks for your hard and crisp work !
ОтветитьGENIUS !! No other words. Such a smooth and clear explanation to the problem
ОтветитьI like to add my humble opinion here. The while loop in the "take()" may allow N number of threads to enter the next line and which is removing the element. The first thread will remove the element successfully but the remaining threads will be ends up in Null. I would suggest using a slightly modified "Observer design pattern".
ОтветитьAt 9th min, where you replaced "if" with a "while", I personally feel its unnecessary, because the second thread will go in await as the queue.size() will be 0. It will not get any null value or I am missing something.
ОтветитьThanks!
ОтветитьOption 3 with semaphore would have been great. Thank you.
ОтветитьI really want to thank you for this video, It taught me a lot of things Blocking queue, await-signal, wait-notify.
But at the same time, the last mistake wasted a lot of time of mine. I should have seen this comment earlier.
If possible, can you please mention this on the video itself. Maybe as some floating suggestion.
You made it so easy to understand. Thank you very much!
Ответитьyou are the best.. please keep up doing the good work..
ОтветитьThanx u bro
very nice explanation.
Thabk you
ОтветитьWhich is the better way of implementation of blocking queue? "Lock and Condition" or "Wait-Notify" and why?
ОтветитьThis channel teaches more about the approach to solving the problem.They don't go to the solution straight away,first they come to common implemented pattern ,than they optimize the solution.
Brilliant