Java thread synchronization details

  • 2020-05-07 19:45:21
  • OfStack

Java thread synchronization basically follows one logic: lock > Modify the -- -- -- -- -- - > Release the lock

1. Synchronize code blocks

Here's an example:


public class SyncBlock {
  static class DataWrap {
    int i;
  }
  
  static class SyncBlockThread extends Thread {
    private DataWrap date;
    
    public SyncBlockThread(DataWrap dataWrap) {
      this.date = dataWrap;
    }
    
    @Override
    public void run() {
      
      for (int i = 0; i < 10; i++) {
        synchronized (date) {
          date.i++;
          try {
            sleep(1);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          System.out.println(getName() + " " + date.i);
        }
      }
    }
  }
  
  public static void main(String[] args) {
    // Multithreaded implementation variable i In turn, add 1 The output 
    DataWrap dataWrap = new DataWrap();
    
    new SyncBlockThread(dataWrap).start();
    new SyncBlockThread(dataWrap).start();
    new SyncBlockThread(dataWrap).start();
  }

}

In the example, you want to output integers in order.

Usually a synchronized block is an object that needs to be locked. 1 is a Shared resource that needs to be accessed concurrently. Any thread will first lock the specified resource before modifying it. Thus, the thread security is guaranteed. In addition, the thread does not relinquish the resource lock when it calls sleep or yield.

2. Synchronous method


public class SyncMethod {
  
  static class DataWrap{
    int i;
    
    public synchronized void valueGrow(){
      i++;
      
      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      System.out.println(Thread.currentThread().getName() + " " + i);
    }
  }
  
  static class SyncMethodThread extends Thread {
    DataWrap dataWrap;
    
    public SyncMethodThread(DataWrap dataWrap){
      this.dataWrap = dataWrap;
    }
    
    @Override
    public void run() {
      for (int i = 0; i < 10; i++) {
        dataWrap.valueGrow();  
      }
    }
  }
  
  public static void main(String[] args) {
    // Achieve sequential growth and output Datawrap In the i
    
    DataWrap dataWrap = new DataWrap();
    
    new SyncMethodThread(dataWrap).start();
    new SyncMethodThread(dataWrap).start();
    new SyncMethodThread(dataWrap).start();
  }

}

The synchronized method is a method decorated with the synchronized keyword. The synchronized method locks the object itself, so when a thread invokes the synchronized method of an object, if another thread invokes other synchronized methods of the object, it still has to wait for the lock of the object to be released, because the object is locked.

3. Synchronous lock

Synchronization is achieved by defining a synchronization lock object, in which case an Lock object is used to act as the synchronization lock.


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SyncLock {

  static class DataWrap{
    Lock lock = new ReentrantLock();
    int i;
    
    public void valueGrow(){
      lock.lock();
      try {
        i++;
        
        try {
          Thread.sleep(1);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " " + i);  
      } finally {
        lock.unlock();
      }
      
    }
  }
  
  static class SyncLockThread extends Thread {
    DataWrap dataWrap;
    
    public SyncLockThread(DataWrap dataWrap){
      this.dataWrap = dataWrap;
    }
    
    @Override
    public void run() {
      for (int i = 0; i < 10; i++) {
        dataWrap.valueGrow();  
      }
    }
  }
  
  public static void main(String[] args) {
    // Achieve sequential growth and output Datawrap In the i
    
    DataWrap dataWrap = new DataWrap();
        
    new SyncLockThread(dataWrap).start();
    new SyncLockThread(dataWrap).start();
    new SyncLockThread(dataWrap).start();
  }

}

Using a lock object to synchronize threads is more flexible. Some locks also have specific functions, among which the more commonly used ReadWriteLock read-write lock and ReentrantLock reentrant lock are available.


Related articles: