Synchronizing (hah, ironic given which lesson we're at) with the server is agonizingly slow and, for whatever insane reason, has a long cooldown. It's less of a problem for these exercises, but earlier on when solving them was pretty fast it was genuinely awful. It's by far the biggest downside of CodeGym's use of dark matter/unlocking lessons. The only times I've been doing these exercises in the IDE are either when they throw a "you must use IntelliJ" error in the browser (pretty rare) or when you have to use multiple files.
This solution has been validated ! and considered better than 60% other students !!!!
static {
president = new OurPresident();
synchronized (president) {
}
}
the "lock" (mutex) is a property of an object, so "this" doesn't refer to a block or code, but to the object that owns that block of code when executing.
"Notes" is an object (List<>) so you want to synchronize only that object, not the whole Note object that owns "notes" (if you use "this").
I share what i managed to find out from others:
synchronized (ClassName.class) : this notation represents the class-level lock instead of the object-level (this).
When a class-level lock takes effect, it results in:
- locks all synchronized static methods, and
- locks all synchronized code blocks marked by ClassName.class.
When a thread enters a synchronized block/method, it also locks all other blocks/methods that are synchronized with the same type.
GO TO FULL VERSION