One, foreword

Have you ever thought about a problem, the Java ++ operation is not thread safe, that is, in the case of multi-threading, multiple threads to execute the ++ operation, the result will be the expected result, we can write a demo to verify.

public static int count = 0;
	
public static void main(String[] args) {
	for (int j = 0; j < 10000; j++) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				add();
			}
		}).start();
	}
	try {
		Thread.sleep(200);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(count);
}

public static void add(){
	count++;
}
Copy the code

The final execution result is shown as follows:Obviously, in Java++Operations are not thread-safe. This concerns how to ensure thread-safety when operating on the same variable or method in a multi-threaded environment.

Second, the Synchronized

What is Synchronized?

Synchronized keyword, is the use of a specific object to set a lock (lock), in the multi-thread concurrent access, only one thread can obtain the lock, and execute the code, until the completion of the code execution, release the lock, and then continue to be seized by other threads.

The use of Synchronized

We could tweak the demo above to make the ++ operation thread-safe.

public static int count = 0;

public static void main(String[] args) {
	for (int j = 0; j < 10000; j++) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				add();
			}
		}).start();
	}
	try {
		Thread.sleep(200);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(count);
}

public synchronized static void add(){
	count++;
}
Copy the code

Just a minor change in the add() methodsynchronizedKeyword, look at the execution result: The result of multiple executions is10000As expected, the Add () method has also become a thread-safe method.

Synchronized scenarios

Synchronized has three different use scenarios, and also corresponds to different lock objects.

1. The Synchronized code block

Modify the add() method of the demo above

public static void add(){ Object o = new Object(); Synchronized (o){count++; }}Copy the code

Define an Object Object, take Object as a lock Object, only one thread will acquire the lock Object, perform the ++ operation, other threads need to wait for the lock Object is released, grab the lock Object, the lock Object can continue to execute ++.

2. The Synchronized methods

public synchronized void add(){ ... . }Copy the code

What is the lock object in the add() method? Obviously, this is the current object, and the thread gets the primary object.

Synchronized static methods

	public synchronized static void add(){
	count++;
}
Copy the code

The last method is the add() method in demo, in which the lock object is the bytecode object of the current class. Since the static method belongs to the class, the lock object is the bytecode object of the current class.

conclusion

Synchronized in a code block, the lock object can be any object. Synchronized in a method, the lock object is the current object, this. Synchronized in static methods, the lock object is the bytecode object of the current class.Copy the code

Synchronized classes are commonly used

Vector is thread-safe,ArrayList and LinkedList are thread-safe, HashSet, TreeSet is unsafe StringBuffer is thread-safe,StringBuilder is thread-safe HashTable is thread-safe,HashMap is thread-safe

Four,

Although Synchronized can ensure the data security of multi-thread shared data, if improperly used, threads will hold the lock object for a long time and it is not released in time, which is easy to cause deadlock problems. Moreover, in terms of performance, Synchronized also has certain impact on performance, so please be careful to use Synchronized.

Thank you for watching, if there is help please click a like to support, thank you.