Java synchronized same object multi-instance thread security
Synchronized is the simplest thread concurrency synchronization syntax in java. The source code is the fastest to understand,
The Resouce class indicates concurrent resources.
Method 1: directly lock a variable, such as the lock in the Code. Note that the lock must be static. What are the consequences if it is not static?
Public class Resource {
Private static Object lock = new Object ();
Private Object unLock = new Object ();
Public void say (){
Synchronized (unLock ){
System. out. println ("come in unlock ");
Synchronized (lock ){
System. out. println ("come in sleep ");
Try {
Thread. sleep (5000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}
Method 2: add the synchroized keyword before the method.
Public synchronized void say (){
System. out. println ("come in sleep ");
Try {
Thread. sleep (5000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
Method 3: Lock the class package com. learn. bao. mutithread;
Public class Resource {
Public void say (){
Synchronized (Resource. class ){
System. out. println ("come in unlock ");
System. out. println ("come in sleep ");
Try {
Thread. sleep (5000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}
Let's talk about the principles in sequence.
Before using the synchroized method, you must understand what resources the key word is locking.
In multithreading, if there is only one Resouce object, the above methods are acceptable.
For example, the following code is acceptable.
Package com. learn. bao. mutithread;
Public class TestThread extends Thread {
Private Resource r;
Public TestThread (Resource r ){
This. r = r;
}
@ Override
Public void run (){
R. say ();
}
}
Package com. learn. bao. mutithread;
Public class Main {
Public static void main (String [] args ){
Resource r = new Resource ();
For (int I = 0; I <3; I ++ ){
TestThread t = new TestThread (r );
T. start ();
}
}
}
Why? Because all threads share a Resouce object and only one instance, it is thread-safe to add the synchroized keyword.
But what should we do below?
Package com. learn. bao. mutithread;
Public class Main {
Public static void main (String [] args ){
For (int I = 0; I <3; I ++ ){
Resource r = new Resource ();
TestThread t = new TestThread (r );
T. start ();
}
}
}
For method 1, if lock is not a static variable, it cannot be shared in the thread, so thread security cannot be implemented. Therefore, the static keyword must be added to the lock variable.
For method 2, because the lock is that Resource is the object itself, if the Resource has multiple objects, thread security cannot be implemented, and thread security can only be ensured on the same object, the above situation cannot be applied. For multiple instances, You need to manually lock the instance or change the method to static.
Method 3 is safe for multiple instances and single instances, because it locks the class Object and has only one unique object in multiple threads, so it is safe, like static lock, this is my Recommended Practice and the code is concise.
The static synchroized static synchronization method actually locks the Resouce class, which has nothing to do with multiple instances, even new Resource (). say (). It is thread-safe to force the creation of new objects to call the static synchronization method.