Advanced Java: Multi-threading Part 7 - Producer-Consumer

preview_player
Показать описание
-------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор

6 Year old video series. One word for this playlist -> "Amazing"

Allah-
Автор

If you want the consumer to only start emptying the queue when the producer finishes, you have to use signals of some kind, or wait and notify. These are covered in other videos in this series. You could also use a CountDownLatch I suppose. To make the producers stop, just check the size of the queue and break the loop if it's big enough. You'd have to think it through carefully though. I think there's no trivial answer. You need more videos :)

caveofprogramming
Автор

Without join(), the main thread would not wait for the two threads to finish. So the main program thread would continue after starting the threads and finish, which would possibly cause the termination of the two child threads too. join() has to be called to make the main program wait for the threads it has created (in this case, it waits indefinitely).

caveofprogramming
Автор

Very short, clear and simple tutorial. This is the best tutorial I have seen on Java multithreading. Thanks

kw-mx
Автор

This is the best tutorial I have seen on Java multithreading. Thanks

roshankumarmutha
Автор

Just make sure that if they access any shared resource (such as the queue), they have to get the lock first. It should be fine. If this is for your own project rather than coursework, it's much easier and better to use ArrayBlockingQueue, then you don't need to worry about synchronization at all and you can get rid of the locks.

caveofprogramming
Автор

Yeah you're right, the producer has time to put one in there, in-between the consumer taking one out and printing the queue size.

caveofprogramming
Автор

It sounds like you are trying to call a method of your main class without having done "new" to create an instance of that class. If you call a method of a class you've created, it must either be a static method, or you must have done "new" somewhere. This applies even to the class where you have your main method.

caveofprogramming
Автор

It means it only performs the code block on average 1 in 10 times, at random.

random.nextInt(10) is equal to 0 only 1 out of every 10 times on average.

caveofprogramming
Автор

If you could dive deep into Advanced Multi-threading by explaining concurrent design patterns like this, observer patter etc(given that the first 14 videos teach a lot on the basics) I think this tutorial would be "complete" :)

Still advanced : Analyzing deadlocks, data-races, ad-hoc synchronizations, modelling using Petrinets and so on :)

Vendettaaaa
Автор

Best tutorials on Multi threading. Great work John!

ankitgupta
Автор

Change 10 to 7 :) And just create more producers. It's that simple!

caveofprogramming
Автор

6 Year old video series. One word for this playlist Amazing

Allah-
Автор

You're most welcome. Actually I got 3 dislikes and 350 likes so far :)

caveofprogramming
Автор

what's up with static methods and the try/catch ? l mean why do we need to make our methods static in order to put the try/catch ?

theodorosandronikou
Автор

A typical performance related question on object creation in loops:
1. I know we are using Integer in our Queue! but in the while loop why not use primitive int for better performance as it is seen as a better way (reduce memory consumption)... not only in this program but in general I have used and seen primitive being used?? Any thoughts on this...

Will for this nice tutorial..

sumitvyas
Автор

Hi John. I have one question. why we use join()? We are having loop with no end, so threads are not finishing. I think that join() will not be called from the program in this case, or i am wrong?

acegulabot
Автор

hi john, do you have a link with the source code? i followed the caveprogramming link, but it seems like the lectures are no longer there. thanks.

g.
Автор

Oh I see what you mean now ... the producer is enqueuing items in that little gap after the consumer takes an item but before it displays the size of the queue.

johnpurcell
Автор

This is a great playlist. I learned a lot so far

Ghettolc
visit shbcf.ru