synchronized notes

Other synchronized method synchronized instance method 1. When a thread is accessing an object, other threads can not access the object

    For each instance method, the lock is applied to the object, when one thread to access a synchronized instance method in which the modified, this thread got a lock object, so other threads can not get a lock on the object, it can not access other synchronized method of that object

2. synchronized modified example of the method, two (or more) threads to get the same lock object, to achieve the correct concurrency.

    A simple +1 thread:

 1 class MyThread implements Runnable {
 2     static int i;
 3 
 4     public int getI() {
 5         return i;
 6     }
 7 
 8     public synchronized void incrI() {
 9         i++;
10     }
11 
12     @Override
13     public void run() {
14         for (int j = 0; j < 1000000; j++) {
15             incrI();
16         }
17     }
18 }

    Two threads get the same object (myThread) locks:

 1 public class SynTest {
 2     public static void main(String[] args) throws InterruptedException {
 3         MyThread myThread = new MyThread();
 4         Thread t1 = new Thread(myThread);
 5         Thread t2 = new Thread(myThread);
 6         t1.start();
 7         t2.start();
 8         t1.join();
 9         t2.join();
10 
11         System.out.println(MyThread.i);
12     }
13 }

3. When acting synchronized static method, the lock which locks the object class is the current class. It is possible to construct two threads with two different objects, but these are two threads to get the same lock (class object lock), it is still possible to achieve the correct concurrency.

1 public synchronized static void incrI() {
2     i++;
3 }

   

 1 public class SynTest {
 2     public static void main(String[] args) throws InterruptedException {
 3         MyThread myThread = new MyThread();
 4         Thread t1 = new Thread(myThread);
 5         Thread t2 = new Thread(new MyThread());
 6         t1.start();
 7         t2.start();
 8         t1.join();
 9         t2.join();
10 
11         System.out.println(MyThread.i);
12     }
13 }

 4. synchronid underlying implementation

    Modified code blocks: monitorenter and monitorexit instructions.

    Test code:

1 public class SynBlock {
2     private int i;
3 
4     public void incr() {
5         synchronized (this) {
6             i++;
7         }
8     }
9 }

    After javap decompile: there will be two exit, the first one is normally performed when you exit, the second is automatically generated for execution abnormal end

    

    Modification method: Use ACC_SYNCHRONIZED logo

    

5. synchronized Optimization: biased locking, lightweight lock

    Biased locking: acquiring the same lock in the same thread several times, without having to apply for a direct acquisition, will help improve performance. It applies to lock less competitive situation.

    Lightweight lock: After biased locking failure, becomes a lightweight lock apply to an alternate execution of multiple threads, less competition.

    Spin lock: will assume soon get locked in the near future thread, so that the current thread short cycle, if you can get the lock, take it directly, otherwise the expansion heavyweight lock.

6 reentrant meaning: a thread that holds the lock request own resources, direct access to the lock, no need to apply again.

7. wait / notify / notifyAll must be synchronized, since this method must get three monitor object, and can ensure get synchronized monitor object.

reference:

https://blog.csdn.net/javazejian/article/details/72828483

Guess you like

Origin www.cnblogs.com/ainsliaea/p/11370018.html