Block Level Sync

What is the value of the parameter passed for synchronization?

synchronized ( parameter ) { } 

to achieve block level synchronization. Somewhere I saw code like

 class test { public static final int lock =1; ... synchronized(lock){ ... } } 

I do not understand the purpose of this code.

Can someone give me a better example and / or explain it?

+3
source share
5 answers

This is a link to the lock. Basically, two threads will not execute code blocks synchronized using the same link at the same time. As Cletus says, a synchronized method is basically equivalent to using synchronized (this) inside a method.

I really hope that the code of the example you saw was not quite like that - you are trying to synchronize with a primitive variable. Synchronization only works on the monitor (via a link), even if it was a legal code, x will be put in a box, which will lead to some very strange behavior, as some integers will always be placed in the same links, and others will create a new object every time you insert. Fortunately, the Java compiler understands that this is a very bad idea and will give you a compile-time error for the code you published.

More reasonable code:

 class Test { private static final Object lock = new Object(); ... synchronized(lock){ ... } } 

I made the lock private and change its type to Object . Regardless of whether it should be static, it depends on the situation - basically a static variable is usually used if you want to receive / modify static data from several streams; instance variables are typically used for locking when you want to access / modify the data of each instance from multiple threads.

+11
source

It:

 public synchronized void blah() { // do stuff } 

semantically equivalent:

 public void blah() { synchronized (this) { // do stuff } } 

Some people do not like to use 'this' for synchronization, partly because they are publicly available (in the case when the instance is visible to the external code). This is why you end up with people using private locks:

 public class Foo private final static String LOCK = new String("LOCK"); public void blah() { synchronized (LOCK) { // do stuff } } } 

The advantage is that LOCK is not visible outside the class, plus you can create multiple locks to work with finer-grained lock situations.

+6
source

The purpose of the synchronized statement (ref: here ) is to make sure that in a multi-threaded application, only one thread can access the critical data structure at a given time.

For example, if you allow two threads to simultaneously change the same data structure, the data structure will be corrupted, then you usually protect it with a lock.

In the real world, consider an analogue where a public toilet has a key hanging in a central place. Before you can use the toilet, you need a key not only to enter, but also a guarantee that no one else will try to enter the same toilet at the same time. They will have to wait until the key becomes available.

Here's how this lock design works.

The synchronized keyword parameter is the key in this case. You lock the key, do what you need to do, then you unlock the key so that others have access to it. If other threads try to lock the key while it is currently blocked by another thread, this thread will have to wait.

+2
source

I cannot explain this since it does not compile. You cannot block a primitive.

Another bad example would be to change it to

 public static final Integer lock =1; 

This is a bad idea, as small cached, automatically loaded primitives are cached and are likely to have strange side effects (if done more than once)

+2
source

An instance of an object that is transferred to synchronized is a "blocking unit." Other threads that are running that are trying to get a lock in one instance are all waiting in line.

The reason people use this rather than the keyword with method-level synchronization (which locks an instance of an instance of a class) is because they may need a smaller or different thing to wait for locks depending on how they are multithreaded algorithm.

+1
source

Source: https://habr.com/ru/post/1444520/


All Articles