Advanced Java: Multi-threading Part 4 -- Multiple Locks; Using Synchronized Code Blocks

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

7 years dude, and still an excellent tutorial; Thank you!.

PajaDeath
Автор

When you use a synchronized block or method, there's always an object that you're synchronizing on. You specify this explicitly with blocks; with methods the object is the object containing the methods. The idea is to make your threads "acquire" the lock associated with this object before running code. It's like passing around a baton before any person in a group can speak. Anyone could just shout out; it's up to you to to enforce the rule that the person must have the baton (lock) first.

caveofprogramming
Автор

2022! Still an awesome tutorial. Whenever I forget the concepts I jump back to this series!

arpanbanejee
Автор

Beautifully explained. Someone who understands well can explain well. Thanks you so much.

shikharsharma
Автор

Thank you so much, this tutorial really helped me with my homework! Appreciate ya!!

SleepyMicky
Автор

No, it's just that using synchronized methods means locking one particular implicit lock all the time (the one belonging to the object that has the methods) and this isn't always what you want. It depends on the circumstances.

caveofprogramming
Автор

Thanks for these videos on concurrency. Some of the best lectures I've found so far!

Nephrotreego
Автор

Hi Karthik, actually, nothing inside the synchronized block is affected in any way at all. It's purely that a thread cannot get into the block unless it first acquires the lock of the object you lock on. So the block is like a building which you cannot get into without a key, but once inside the building, everything happens in the normal fashion (whether in regard to primitive types or anything else).

caveofprogramming
Автор

Thanks for the tutorial. Explaining with simplicity is an art. Can you please share more views on why did you use separate lock objects rather than locking lists?

vikrantkaushal
Автор

Would we get the same result, had we used Vector (thread safe collection) instead of ArrayList?

pkaramol
Автор

Congratulations! I've finally understood this concept! Thank you!

sonnix
Автор

Really useful techniques you are showing here John. Very clearly presented too. Thank you!

Laharnaman
Автор

There's a separate App.java that contains the main method. You can find the full source code if you scroll down to the multithreading section on the home page of Cave of Programming.

caveofprogramming
Автор

I missed some code off from the Cave of Programming page, apologies. I've added it now. You need a class with a main method that just creates a Worker object and calls the method called main() (which is not a "real" main method)

caveofprogramming
Автор

Best videos I've found so far. Question, on this particular example why use threads to end with the same execution time that without them on the first place?

einarabelc
Автор

Hmm... I see... and I have to commend you for your interaction with your viewers, not everyone is so helpful. I look forward to watching more of your content!

MAGNUMandBARRETTA
Автор

Hello thanks for the great tutorials, but I was wondering can't you just make one thread execute stageone and another execute stagetwo and that way the both methods are run at the same time by different threads and there would be no problems? Thanks in advance

fkangalov
Автор

I think the lock in synchronized block explanation was really cool

charatkakkar
Автор

Hey John, very well done. It's wonderful tutorial. I got all the answers which I wanted to know.... it's one stop point to learn thread tutorial. Thank you... 

manjunath
Автор

Question for the last version of code using synchronized blocks: How can a thread after getting blocked in the first method's synced block continue to do second method's synced block and then return to the previous one, if the process method explicitly says 1) stageOne 2)stageTwo. e.g. how does she now there are other blocks in advance? Thanks

DMJNTTK