Advanced Java: Multi-threading Part 9 - A Worked Example Using Low-Level Synchronization

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

You produce a very good line of tutorials. You have made almost all of my Multithreading concepts clear. Thanks.

dharmikkachhia
Автор

Good question .... This code is safe, because list.size() is only accessed from within synchronized blocks and synchronized blocks are guaranteed to be visible to all threads --- volatile isn't necessary anywhere.

caveofprogramming
Автор

You need a loop to keep reading items and to keep producing items. Otherwise it would read only one or produce only one. But it doesn't need to be infinite (and shouldn't, in a real application). You could use the technique from video 2 to stop the loops, or you could use "for" loops, or make the application quit when a particular item is received, etc.

caveofprogramming
Автор

It's really just in case the linked list is re-assigned. It's considered good practice. Also, with some objects (e.g. numbers and strings), it's not always completely clear when two references actually point to identical objects.

caveofprogramming
Автор

I'm not sure if Vector eliminates the need for synchronisation. I gather it has some issues, such as synchronizing single operations only rather than iterations. As for volatile, it still wouldn't be useful, because (according to my understanding) making a reference volatile will have no effect on the class the reference points to, only on the reference itself. So volatile is of very limited usefulness.

caveofprogramming
Автор

Thanks a ton thanking you for such a neat seen different videos on youtube ..but yours is above all...

ashmitnk
Автор

So good explanation! Excellent. And very cultural music. I feel yourself like student in the Hogwarts School of Witchcraft and Wizardry studying magic of Java multithreading )

Boyarsskiy
Автор

To use an extra object to lock -- whether an actual Lock object or just an Object (the latter style is probably a bit old). But just don't use the actual object that you're locking as the lock. Thanks for your comments, by the way! -John

caveofprogramming
Автор

There's a main method there. Often I put it in a separate class to keep things simple (but maybe it makes it more confusing!! :))

caveofprogramming
Автор

There are two threads; you're right the first time. One runs the produce method, the other runs the consume method.

caveofprogramming
Автор

What is best practices? Always to lock on common Java inbuilt "lock" object or to Object that we are manipulating to?

dharmikkachhia
Автор

Hi John,

Please clear my doubt.

You called sleep in the cosumer, it just makes the consumer to sleep, at the same time..the other thread does not run too...means the producer does not produce the data while the consumer thread is sleeping..pls clarify

PuneetKhanna
Автор

Excellent work John ... You have explained advance things in a very simple way :)

MrYou
Автор

About if we skip synchronized blocks and use Vector instead of LinkedList (and delete lock object and waits and notify). Should we use volatile keyword anywhere?

TheMeehaw
Автор

Hi John, isn't the code a deadlock situation ? Right now I guess the Thread.sleep() from consumer is avoiding it. Please correct me if I am wrong.
I have seen u have covered the Deadlock in upcoming tutorials, but that is through JAVA Concurrency APIs. Could you please help me how it used to be handled before JAVA 5?

alearnrc
Автор

Hi john thx for your explanation but I am still confused about the fact that the size of the list is 2 when you display.than you

edgardzafack
Автор

Your tutorials are the best. You are amazing!

santoshdas
Автор

You can check them in previous tutorials or on his website.

sriramsantosh
Автор

In producer, when lock.wait() is called. Does the producer release the lock and enter "waiting" state?

sbylk
Автор

It's not really needed; I just put it in to slow things down in a slightly random fashion --- simulating the way you'd take things off a queue if each item took some time to process.

caveofprogramming