This code was included in the link below, but there was no explanation for it.
Does this keep the whole class out of the other threads?
I mean: the whole class = all static methods are locked? But only the static ones?
https://codegym.cc/quests/lectures/questcore.level07.lecture03
What does it mean: synchronized (MyClass.class) ?
Resolved
Comments (11)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
6 May 2021, 13:38
It's a class wide lock instead of an object level lock. Whenever a class wide lock is held it locks all synchronized static methods as well as any synchronized blocks that are locked with the class.
So if you had a class that had both static and non static methods:
- Using 'this' (or just adding synchronize to a non static method) would lock a particular object of that class and no threads would be able to access any blocks locked with 'this' or the non-static methods within. Class level methods (static) can still be accessed.
-Using {AnyClass}.class (or synchronize on a static method) locks the class and no additional threads can access blocks locked with the {AnyClass}.class lock or synchronized static. Object level (Non-static) methods can still be accessed.
0
Gellert Varga
6 May 2021, 18:30
Thank You, but please confirm that I have understood correctly:
synchronized (ClassName.class) { . . . } - this is just a simple notation-form used for locking a specified static block.
It does not lock all the static methods in the class, but only this one method/codeblock on which we have placed this notation.
+1
Guadalupe Gagnon
6 May 2021, 18:37
it will lock any static method that is synchronized as well as any block that is locked with the same class level lock. So if one thread held a class wide lock somewhere else in code then:
0
Gellert Varga
6 May 2021, 20:46
There is a synchronized block locked with a class lock inside an instance method (in myMethod3)?
I can't understand what does this means.
+1
Guadalupe Gagnon
6 May 2021, 20:50
what don't you understand? It's valid code to have a class locked block in an instance method. You can't have an object locked block of code is a static method though because static methods aren't part of any instance.
Have you ever played doom from the early 90's? They have color coded doors that are locked until you pick up the corresponding color coded key. This is very similar except that its locking instead of unlocking. When a thread enters a synchronized block/method it locks ALL the other blocks/methods that are locked with the same type (object level, class level, or a even specific object) no matter where they appear.
0
Gellert Varga
6 May 2021, 21:22
This was the key for me:
"When a thread enters a synchronized block/method it locks ALL the other blocks/methods that are locked with the same type (object level, class level, or a even specific object) no matter where they appear."
It's clear now.
+1
Guadalupe Gagnon
6 May 2021, 21:26
=)
If your head is spinning now trying to wrap your head around concurrency, let me assure you that after three years of programming Java (with 5 years prior programming experience on top of that), I still don't fully understand it.
0
Gellert Varga
6 May 2021, 21:34
:-)
These are "encouraging" prospects...
0
Gellert Varga
6 May 2021, 21:41
But one more question from this lecture: https://codegym.cc/quests/lectures/questcore.level07.lecture03.
They write in the example:
Code of the first thread: (inside a synchronized block!)
1 String s1 = name1;
2 name1 = name2;
3 //other thread is running
4 name2 = s1;
Code of the second thread: (inside a synchronized block!)
1 //the thread waits until the mutex is unlocked
2 String s2 = name1;
3 name1 = name2;
4 //other thread is running
5 //other thread is running
6 name2 = s2;
But they write in the lecture: "No other thread can enter this block until our thread leaves it."
But then how is it possible to have such lines: //other thread is running ??
Second thread can't enter this block until first thread finishes it!
0
Guadalupe Gagnon
6 May 2021, 22:44
The first box on that describes the code working in order:
String s1 = name1; //Ally
name1 = name2; //Lena
name2 = s1; //Ally
String s2 = name1; //Lena
name1 = name2; //Ally
name2 = s2; //Lena
The second two boxes describe the events, but the don't mean that they are happening consecutively.
Check the example from 2 lessons ago, the one with the same code but without the synchronizes:
Level7 lesson 1
It shows the sequence as:
String s1 = name1; //Ally
name1 = name2; //Lena
String s2 = name1; //Lena(!)
name1 = name2; //Lena
name2 = s1; //Ally
name2 = s2; //Lena
0
Gellert Varga
7 May 2021, 12:44
OK, then I'd rather not deal with the second two boxes. The point is that the two threads do their own work one after the other, in the expected order.
0