Java syntax: "synchronized (this)"

Could you explain this piece of Java code to me? I can not understand this syntax.

synchronized (this) { try { wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } } 
+4
source share
6 answers

This means that this synchronized code block means that no more than one thread will have access to the code inside this block.

Also this means that you can synchronize the current instance (get a lock in the current instance).

This is what I found in Katie Sierra's certification book.

Since synchronization damages concurrency, you do not want to synchronize more code than is necessary to protect your data. Therefore, if the scope of the method is more than necessary, you can reduce the volume of the synchronized part to something less than the full method - just a block.

Take a look at the following code snippet:

 public synchronized void doStuff() { System.out.println("synchronized"); } 

equivalent to this:

 public void doStuff() { //do some stuff for which you do not require synchronization synchronized(this) { System.out.println("synchronized"); // perform stuff for which you require synchronization } } 

In the second fragment, you lock synchronization only for the necessary code, and not to lock synchronization for the entire method.

+8
source
 synchronized (this) 

is the syntax for implementing block level synchronization.

This means that only on this object and only one thread can call a closed block at a time.

See a more detailed answer: Block Level Lock

+1
source
 synchronized (this) { } 

Its called a synchronized block, and it can choose which object it synchronizes to. A synchronized method can only use 'this' (or the corresponding class instance for a synchronized class method)
Check out these links, maybe this will give you a better answer.

0
source

synchronized (this) - We get a lock associated with the object that this points to. When we use this block, we mean that we are ready to wait until the thread using this monitor releases it. It makes sense to use a lock if you modify a data object (object variables).

wait - makes the current executable thread wait until another thread calls the notification method or for the specified duration.

0
source

This first line controls concurrent access to a closed block of code. Only one thread at a time can execute a block of code at a time. Read more in section 2.2 of this tutorial.

 synchronized (this) { 

The attached code block below uses a (very bad) method to pause the execution thread for a certain amount of time.

  try { wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } 

In addition, he โ€œswallowsโ€ any exceptions that may be thrown during the wait, which is really very naughty.

0
source

In most situations, only one thread can access "Synchronized (this)" at a time. But it's not always the case! Consider the code below. In my code, I synchronized the static object as a lock, and everything works fine. When I change the code for synchronization (this), several threads access the code at the same time, and this will ruin everything. Be careful when synchronizing (this). Note. The code creates threads recursively and then displays them in reverse order.

home

 public class Project2 { public static void main(String[] args) throws InterruptedException { Object lock = new Object(); Threads2 t1 = new Threads2(1); t1.setName("Thread 1"); t1.start(); Threads2.que.add(t1); t1.join(); } } 

Threads2

 public class Threads2 extends Thread { //private final ArrayBlockingQueue<Threads> que = new ArrayBlockingQueue<>(5); public static ArrayList<Thread> que = new ArrayList<>(5); private volatile int i; private static volatile boolean locked = true; private static final Object lock = new Object(); public Threads2(int i) { this.i = i; } @Override public void run() { try { System.out.println("Thread " + i + ": created"); synchronized(lock) { i++; } if(i <= 50) { Threads2 t = new Threads2(i); t.setName("Thread " + i); t.start(); que.add(Thread.currentThread()); } else { Thread.currentThread().interrupt(); } while(locked) { Thread.sleep(10000); } } catch(InterruptedException ex) { synchronized(lock) { if(i >= 0) { que.get(i-2).interrupt(); i--; System.out.println(Thread.currentThread().getName()); } } } } } 
0
source

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


All Articles