synchronized complex example
Category 1: Object Lock Instances
1.1 Object locks for the same instance
Asynchronous method, synchronous method, synchronous statement access instance for an instance (object)
Instance class code
//Asynchronous, synchronous instance functions, synchronous object (this)Statement statements public class SyncThread implements Runnable{ @Override public void run() { //currentThread():Returns a reference to the currently executing thread object //getName():Returns this thread's name //Name judgment for incoming threads, and different functions are executed separately //(Asynchronous instance function, synchronous instance function, synchronous statement in asynchronous instance function) String threadName = Thread.currentThread().getName(); if (threadName.startsWith("A")){ try { async(); } catch (InterruptedException e) { e.printStackTrace(); } }else if (threadName.startsWith("B")){ try { syncStatemnet(); } catch (InterruptedException e) { e.printStackTrace(); } }else if (threadName.startsWith("C")){ try { syncInstance(); } catch (InterruptedException e) { e.printStackTrace(); } } } /** * Asynchronous Instance Function: Multiple threads can access asynchronous (asynchronous) function async() * @throws InterruptedException */ private void async() throws InterruptedException { //Print, thread name + start (time after formatting) System.out.pr1intln(Thread.currentThread().getName()+"_Async_Start:"+new SimpleDateFormat("HH:mm:ss").format(new Date())); Thread.sleep(2000); //Print, thread name + end (time after formatting) System.out.println(Thread.currentThread().getName()+"_Async_End:"+new SimpleDateFormat("HH:mm:ss").format(new Date())); } /** * Synchronized Instance Function: Each thread executing this function needs a line to get the object lock to execute, otherwise it will block (wait / hang) * @throws InterruptedException */ private synchronized void syncInstance() throws InterruptedException { System.out.println(Thread.currentThread().getName()+"_SyncInstance_Start:"+new SimpleDateFormat("HH:mm:ss").format(new Date())); Thread.sleep(2000); System.out.println(Thread.currentThread().getName()+"_SyncInstance_End:"+new SimpleDateFormat("HH:mm:ss").format(new Date())); } /** * Synchronization statement in an asynchronous function: This function is asynchronous, but there is a synchronization statement in it. * All threads can access this function first, but when executing the synchronization statement, they need to get the object lock first, otherwise they will block. * @throws InterruptedException */ private void syncStatemnet() throws InterruptedException { //Asynchronous Statement in Asynchronous Instance Functions System.out.println(Thread.currentThread().getName() + "_SyncStatemnet()_HasIn:" + new SimpleDateFormat("HH:mm:ss").format(new Date())); //Synchronized Statement in Asynchronous Instance Functions synchronized (this) { System.out.println(Thread.currentThread().getName() + "_SyncStatemnet_Start:" + new SimpleDateFormat("HH:mm:ss").format(new Date())); Thread.sleep(2000); System.out.println(Thread.currentThread().getName() + "_SyncStatemnet_End:" + new SimpleDateFormat("HH:mm:ss").format(new Date())); } } }
Test class main function code
public class SyncTest { public static void main(String[] args) { SyncThread syncThread = new SyncThread(); //A Common instance function, asynchronous [accessible by multiple threads at the same time] Thread A_thread1 = new Thread(syncThread, "A_thread1"); Thread A_thread2 = new Thread(syncThread, "A_thread2"); //B modifies Statement, Synchronization [Synchronization Statement will be exclusive to threads] Thread B_thread1 = new Thread(syncThread, "B_thread1"); Thread B_thread2 = new Thread(syncThread, "B_thread2"); //C modifies instance functions, synchronization [synchronization instance functions will be exclusive to threads] Thread C_thread1 = new Thread(syncThread, "C_thread1"); Thread C_thread2 = new Thread(syncThread, "C_thread2"); A_thread1.start(); A_thread2.start(); B_thread1.start(); B_thread2.start(); C_thread1.start(); C_thread2.start(); }
Result analysis
Double-click to zoom in
Results Analysis 1:
Two threads of the same object, accessing asynchronous functions, are executed simultaneously
Two threads of the same object can not access the synchronous instance function at the same time. They need to wait for the last thread to complete before the next thread can execute.
Two threads of the same object can not access synchronous statements at the same time, but the statements of asynchronous statements are still asynchronous statements, which are executed at the same time.
1.2 Object Locks for Different Instances
For different instances (objects), access their respective asynchronous and synchronous methods to state instances
Instance class code
Same 1.1 instance class code
Test class main function code
Modify 1.1 test class code
public class SyncTest { public static void main(String... args) { //Delete SyncThread syncThread = new SyncThread(); //new a different SyncThread object for each thread. Thread A_thread1 = new Thread(new SyncThread(), "A_thread1"); Thread A_thread2 = new Thread(new SyncThread(), "A_thread2"); Thread B_thread1 = new Thread(new SyncThread(), "B_thread1"); Thread B_thread2 = new Thread(new SyncThread(), "B_thread2"); Thread C_thread1 = new Thread(new SyncThread(), "C_thread1"); Thread C_thread2 = new Thread(new SyncThread(), "C_thread2"); A_thread1.start(); A_thread2.start(); B_thread1.start(); B_thread2.start(); C_thread1.start(); C_thread2.start(); } }
Result analysis
//console A_thread1_Async_Start:23:55:37 C_thread2_SyncInstance_Start:23:55:37 B_thread2_SyncStatemnet()_HasIn:23:55:37 A_thread2_Async_Start:23:55:37 C_thread1_SyncInstance_Start:23:55:37 B_thread1_SyncStatemnet()_HasIn:23:55:37 B_thread2_SyncStatemnet_Start:23:55:37 B_thread1_SyncStatemnet_Start:23:55:37 A_thread1_Async_End:23:55:39 C_thread2_SyncInstance_End:23:55:39 A_thread2_Async_End:23:55:39 C_thread1_SyncInstance_End:23:55:39 B_thread2_SyncStatemnet_End:23:55:39 B_thread1_SyncStatemnet_End:23:55:39
Results Analysis 2:
Two threads accessing synchronized(this|object) {} code blocks of different objects and synchronized decorated non-static methods are asynchronous, because object locks are for one object, and object locks of multiple objects exist independently of each other.
Object locks of different objects (instances) of the same class do not interfere with each other and are independent of each other.
Category 2: Class lock instances
2.1 Class locks for the same instance
Instance class code
Modify SyncThread class's synchronous instance method to synchronous static (class) method
And modifying Synchronized Statement this as SyncThread.class
The code for the modification section is given below.
//Change Synchronized Instance Method to Synchronized Static (Class) Method //static modifier private synchronized static void syncInstance() throws InterruptedException { System.out.println(Thread.currentThread().getName()+"_SyncInstance_Start:"+new SimpleDateFormat("HH:mm:ss").format(new Date())); Thread.sleep(2000); System.out.println(Thread.currentThread().getName()+"_SyncInstance_End:"+new SimpleDateFormat("HH:mm:ss").format(new Date())); } //this is changed to SyncThread.class. private void syncStatemnet() throws InterruptedException { System.out.println(Thread.currentThread().getName() + "_SyncStatemnet()_HasIn:" + new SimpleDateFormat("HH:mm:ss").format(new Date())); //Change this to SyncThread.class synchronized (SyncThread.class) { System.out.println(Thread.currentThread().getName() + "_SyncStatemnet_Start:" + new SimpleDateFormat("HH:mm:ss").format(new Date())); Thread.sleep(2000); System.out.println(Thread.currentThread().getName() + "_SyncStatemnet_End:" + new SimpleDateFormat("HH:mm:ss").format(new Date())); } }
Test class main function code
Test class main function code for object locks of the same instance as 1.1
Result analysis
//console B_thread2_SyncStatemnet()_HasIn:00:38:59 C_thread2_SyncInstance_Start:00:38:59 B_thread1_SyncStatemnet()_HasIn:00:38:59 A_thread2_Async_Start:00:38:59 A_thread1_Async_Start:00:38:59 A_thread2_Async_End:00:39:01 C_thread2_SyncInstance_End:00:39:01 B_thread1_SyncStatemnet_Start:00:39:01 A_thread1_Async_End:00:39:01 B_thread1_SyncStatemnet_End:00:39:03 B_thread2_SyncStatemnet_Start:00:39:03 B_thread2_SyncStatemnet_End:00:39:05 C_thread1_SyncInstance_Start:00:39:05 C_thread1_SyncInstance_End:00:39:07
Result analysis:
Asynchronous method (B_thread1-2_SyncStatemnet()_HasIn, A_thread1-2_Async_Start) starts at the same time
Although A1, A2, B1, B2 start at the same time (of course B1, B2 start with asynchronous statements, and synchronous statements are not running), A1, A2 end at the same time after 2s, and all class methods and class statements need to be executed in a queue.
Synchronized static (class) method C2 (Start:00:38:59) starts first, 2s then C2 (End:00:39:01) ends.
Then the static statement B1 (Start:00:39:01) starts again, and B1 (End:00:39:03) ends after 2s.
Then statically state B2 (Start:00:39:03) and start again. After 2s, B2 (End:00:39:05) ends.
Then static (class) method C1 (Start:00:39:05) starts again, and C1 (End:00:39:07) ends two seconds later.
One parking lot exit, four cars queued to pay. It took 38:59-39:07, 8s.
That is to say, the class method of the same instance and the class statement of obtaining. class are all in a queue to execute their own code.
The instance method of the same instance is the same as that of the object lock, and the instance statement of obtaining this is the same.
2.2 Class Locks for Different Instances
Instance class code
Ibid. 2.1 Example class code of class lock for the same instance
Test class main function code
Test class main function code for object locks of the same 1.2 different instances
Result analysis
Test results:
//console A_thread1_Async_Start:00:42:31 B_thread2_SyncStatemnet()_HasIn:00:42:31 C_thread1_SyncInstance_Start:00:42:31 B_thread1_SyncStatemnet()_HasIn:00:42:31 A_thread2_Async_Start:00:42:31 A_thread1_Async_End:00:42:33 C_thread1_SyncInstance_End:00:42:33 B_thread1_SyncStatemnet_Start:00:42:33 A_thread2_Async_End:00:42:33 B_thread1_SyncStatemnet_End:00:42:35 B_thread2_SyncStatemnet_Start:00:42:35 B_thread2_SyncStatemnet_End:00:42:37 C_thread2_SyncInstance_Start:00:42:37 C_thread2_SyncInstance_End:00:42:39
Result analysis:
Two threads accessing different objects synchronized (class. class) state that {} or synchronized modified static methods are synchronized. For different objects of the same class, class locks are the same, and class locks are shared by different objects of the same class.
Class 3: Class lock object locks are available
A class has both object locks (this / instance method) and class locks (. class / static method)
Same class and same object
How will the same object affect?
conclusion
A class can have synchronized modified instance methods (object locks) and static methods (class locks).
These two synchronization methods (class/object lock) can be executed/accessed simultaneously through different threads of the same object.
Different objects of the same kind
How will the two objects affect?
conclusion
The object lock method of two objects of the same class is executed simultaneously, and the class lock method of two objects of the same class is executed in a queue.
summary
Object locks and class locks are independent of each other and work in the same nature, but with different jurisdictions.
Object lock is a pipe instance function and case statement, class lock class function and class statement.