[Reprint] synchronized, volatile, Lock Detailed

synchronized、volatile、Lock详解

https://blog.csdn.net/u012102104/article/details/79231159

 


  In Java concurrent programming process, we will inevitably encounter synchronized, volatile and lock, which lock is a class, while the remaining two are Java keywords. The following records Little Bo development process to understand these three, deficiencies Pleased to meet you.

Angle on threads and processes refer to the blog to tell the operating system threads and processes

the synchronized
  the synchronized keyword in Java, it is a synchronous lock. There are several uses:

1, a modified method: After the range operator, prior to use the return type declaration. Every time there is only one thread to enter the process, then the thread obtained is a member of the lock.

public synchronized void syncMethod() {
    //doSomething
}

 


2, modify the code block: Only one thread to enter the block, then the thread obtained is a member of the lock.

public int synMethod(int arg){
    synchronized(arg) {
        //doSomething
    }
}

3, modified objects: if the current thread to enter, then the other threads take any action on the class of all objects can not be carried out, this time to get the current thread is the object lock.

public class SyncThread implements Runnable {
    public static void main(String args[]) {
        SyncThread syncThread = new SyncThread();
        Thread therad1 = new Thread(syncThread, "therad1");
        Thread therad2 = new Thread(syncThread, "therad2");
        Thread therad3 = new Thread(syncThread, "therad3");
        therad1.start();
        therad2.start();
        therad3.start();
    }
    public void run() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName());
        }
    }
}

 


4, modified class: If the current thread to enter, then all other threads in this type of operation can not be carried out, including static variables and static methods, this time to get the current thread is the object lock.

public class syncClass {
public void method() {
synchronized(syncClass.class) {
//doSomething
}
}
}


volatile
  effect of the volatile keyword is prohibited instruction reordering, variable force values acquired from the common stack, rather than take the value of a variable from the thread private data stack.
volatile synchronized with the following differences:

volatile thread blocking does not occur, and synchronized thread blocking would occur.
only modified variable volatile, and synchronized methods may be modified, and other code blocks.
volatile does not guarantee atomicity (not thread-safe), and can be synchronized to ensure atomicity.
The solution is in the volatile multi-thread between the visibility of variables, and synchronized solution is to access resources between multiple threads synchronization.
lock
  the synchronized implicit locks, this added control in a subject in need of synchronization, the lock is a lock, the display need to specify start and end positions.


It must release the lock finally in use lock, or likely to cause thread deadlock; and the use of synchronized, to acquire the lock after executing thread synchronization code to release the lock (or JVM will release the lock thread execution when an exception occurs).
When using thread lock will not have to wait; and when using synchronized, assuming that the A blocked thread to acquire a lock, other threads will wait.
reentrant lock, can interrupt, fair or not fair; and synchronized reentrant but not interrupted, unfair.
----------------
Disclaimer: This article is CSDN blogger "erudite de beast called" original article, follow the CC 4.0 BY copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/u012102104/article/details/79231159

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12165053.html