9. 8 Lock phenomenon A thorough understanding of locks (there are only two things for locks, one is an object, and the other is a Class template (static modification))

8Locking phenomenon A thorough understanding of locks (there are only two locks, one is an object, and the other is a Class template (static modification))

The first question: (The two methods of the resource class are syn added, when there is only one object) (Send a text message first and then make a call)

 

 

Among them, the printed one must have sent a text message first, and then made a phone call.

Let's talk about the logic here: this is because of the lock. First of all, we need to know what we are locking. The locking is our calling object, which is our new object in main. There is only one object, so whoever gets the lock first will execute it first, so it is to send a text message first and then make a call

Question 2: (We still only have one object, and then let the method called by the first thread wait for 4 seconds before outputting) - (Send a text message first and then call)

 

 

 

Logic: Should the output phenomenon wait for 4 seconds first or send a text message first, and then make a call (we need to know that there is still a lock problem here, we only have one object locked here, so whoever gets the lock first will be the first) executed)

Question 3: (We are giving him an unlocked method in the resource class. We let one thread call the syn method (this method has a sleep for 4 seconds), and one thread calls the unlocked method (hello method) ) (Here, hello will be output first, and then text messages will be output)

 

 

Logic: The phenomenon here is to print out hello first and then wait 4 seconds to print out the outgoing message. Our hello method here does not have the syn lock, so it is not affected by this lock. (And our lock method here is to sleep for 4 seconds before executing)

Question 4: (We prepare two objects, one thread for object 1 to call and send text messages (still sleeping for 4s), and one thread for object 2 to call and call) (here is to call first and then wait for 4s to output text messages)

Logic: The phenomenon here is to call and wait for 4s to output and send text messages. The reason is that these two objects are two locks, so they are executed according to time. Our method of object 1 sleeps for 4s first.

 

 

Question 5: (two static syn synchronization methods (static), only one object to call the method) (wait 4s to send a text message, and then make a call)

.

 

 

 

Logic: The phenomenon here is to wait for 4s output to send text messages (the method is to sleep for 4s first), and then make a phone call. Here our lock method is modified by static. This static modified thing is loaded when the class is loaded (as soon as the class is loaded, there will be a Class template), and then the syn lock is the Class template of the object.

Question 6: (two static modified syn lock methods, two objects call a method respectively) (still wait for 4s to send a text message, and then make a call)

 

 

Logic: The phenomenon is to wait for 4s to send a text message, and then make a phone call. Although there are two objects here, the lock of the resource class is modified by static, and the lock is the Class object (template) that has just been loaded. A class has only one template (new objects are different, but the template of the object is the same). So what we lock is the Class template. Then whoever gets the lock first will execute it first.

Question 7: (a static synchronous method (static syn (sleep 4s)), a common synchronous method (syn), and then only one object, used by two threads) (call first, then wait for 4s to output text messages)

 

 

 

Logic: The phenomenon here is to make a call first and then wait for 4s to output and send a text message. The logic here is that the above static modified syn locks the Class template and needs to sleep for 4s before outputting, while the ordinary synchronization method of our syn modified below locks It is our new object (instance object). The two locks are not the same thing. And the previous static syn has to sleep for 4s first, so it must be the following common syn method to execute first (according to the time)

Question 8: (a static synchronous method (static syn (sleep 4s)), a common synchronous method (syn), and then two instance objects) (still make a call first, then wait 4s before sending a text message)

 

 

 

Logic: The phenomenon is still to make a phone call first, and then wait for 4 to send a text message. It is the same as question 7. The previous static syn locks the Class template (it needs to sleep for 4s first), but the common synchronization method of syn locks object 2. So the two locks are not the same thing, the value depends on the execution time, and the static syn method must first sleep for 4s before outputting

Guess you like

Origin blog.csdn.net/logtcm4/article/details/127859602