Favorite interviewer asked five kinds of usage Synchronized, you know a few?

Foreword

We all know Synchronized thread-safe synchronization, most programs will probably only use synchronization method above. In fact, Synchronized can be used more occasions, lists several usages.

1, the conventional method Synchronization

This is why we use the most, as far as thread-safe way to give up a synchronization lock. Although this method is the easiest to use, but only in a single action embodiment above, if not a single embodiment, lock synchronization method will fail.
/**
 * 用在普通方法
 */
private synchronized void synchronizedMethod() {
    System.out.println("synchronizedMethod");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}


复制代码
In this case, the same instance only one thread can acquire the lock to enter this method.

2, synchronous static method

Synchronous static methods, no matter how many instances of the class, only one thread can acquire the lock to enter this method.
/**
 * 用在静态方法
 */
private synchronized static void synchronizedStaticMethod() {
    System.out.println("synchronizedStaticMethod");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}


复制代码
Synchronous static methods are class-level lock, once a thread enters any of this method, all other threads will not be able to access any synchronization Locks of this type of approach.

3, synchronization classes

The following provides two methods of synchronization classes, pinning effect and synchronous static methods, are class-level locks, only one thread can access method with synchronous class lock.
/**
 * 用在类
 */
private void synchronizedClass() {
    synchronized (TestSynchronized.class) {
        System.out.println("synchronizedClass");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 用在类
 */
private void synchronizedGetClass() {
    synchronized (this.getClass()) {
        System.out.println("synchronizedGetClass");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


复制代码
Two uses here is the use of synchronous blocks, represented here only to get the class lock to enter this code block.

4, examples of this synchronization

This usage is synchronized block, lock the entire current object instance represents only get to this instance of the lock to enter this method.
/**
 * 用在this
 */
private void synchronizedThis() {
    synchronized (this) {
        System.out.println("synchronizedThis");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


复制代码
Common usage and synchronization methods lock, are locked throughout the current instance.

5, the synchronization object instance

This is also the usage of sync blocks, and lock the current instance of the above, as shown here LOCK lock the entire object instance, only to get this instance LOCK lock to enter this method.
/**
 * 用在对象
 */
private void synchronizedInstance() {
    synchronized (LOCK) {
        System.out.println("synchronizedInstance");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


复制代码
Further, the class does not lock the lock block each Example, but the same type locks, the lock current instance of the same, the same object will block lock each other.

At last 

I welcome everyone's attention the public Herd number [programmer], in which the article will be updated, data compilation will be on the inside.


Guess you like

Origin juejin.im/post/5dc031ebf265da4cfd2962e6