Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the eight situations of synchronized Lock in java?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What this article shares to you is about the eight situations of synchronized Lock in java. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Without saying much, let's take a look at it.

Summary and explanation of 8 situations of Lock (local synchronization) lock:

* title:

* 1. Standard access, would you like to print email or SMS Email first?

* A 4-second pause has been added to the 2.email method. Do you want to print email or SMS Email first?

* 3. Add a common hello method, please print the mail first or hello hello

* 4. Two mobile phones, please print email or SMS SMS first.

* 5. Two static synchronization methods, one mobile phone, please print email or SMS Email first

* 6. Two static synchronization methods, two mobile phones, please print email or SMS Email first

* 7. A common synchronization method, a static synchronization method, a mobile phone, please print email or SMS SMS first

* 8. A common synchronization method, a static synchronization method, 2 mobile phones, please print email or SMS SMS first

*

* lock1, 2

* if there are multiple synchronized methods in an object, at a certain time, only one thread calls one of the synchronized methods

* other threads can only wait, in other words, only one thread can access these synchronized methods at some point

* the this of the current object is locked. After it is locked, other threads cannot enter other synchronized methods of the current object.

*

* lock3, 4

* adding a common method has nothing to do with synchronization locks

* after changing to two objects, the lock is not the same, and the situation changes immediately.

*

* lock5, 6

* after being replaced with static synchronization methods, the situation changes (static locks are Class class objects)

* if the ordinary synchronization method, new this, or a specific mobile phone, all ordinary synchronization methods use the same lock-the example object itself

* if the static synchronization method, static class, the only template

* synchronized is the basis for synchronization: every object in Java can be used as a lock

* it is shown in the following three forms.

* for normal synchronization methods, the lock is the current instance object. It is equivalent to for synchronous method blocks, locks are configured objects in synchronized parentheses.

* for static synchronization methods, the lock is the Class class meta information of the current class

*

* lock7, 8

* when a thread tries to access a synchronous code block, it must first get the lock, and the lock must be released when it exits or throws an exception

*

* all synchronization methods use the same lock-instance object itself, which is the concrete instance object itself from new

* that is, if the ordinary synchronization method of an instance object acquires the lock, the other ordinary synchronization methods of the instance object must wait for the lock to be released by the method that acquires the lock.

* however, the ordinary synchronization method of another instance object does not have to wait for the ordinary synchronization method of the instance object to acquire the lock because it uses a different lock from the ordinary synchronization method of the instance object.

* synchronous methods release locks to acquire their own locks

*

* all static synchronization methods use the same lock-the class object itself, which is the only template we talked about, Class.

* specific instance object this and unique template Class. These two locks are two different objects. There is no race condition between all static synchronization methods and ordinary synchronization methods.

* but once a static method acquires the lock with the same method, other static synchronization methods must wait for the method to release the lock before acquiring the lock.

Lock1, 2, 3 an instance object

Lock4, 5, 6, 7, 8 two instance objects

Lock1

1. Standard access, would you like to print email or SMS first? Answer: Email

Both sendEmail () and sendSms () are common synchronization methods and both use synchronized locks

Here, it is executed in the order of method calls. The current synchronized locks the same instance object.

Package day02_lam; import java.util.concurrent.TimeUnit; class Phone {public synchronized void sendEmail () throws InterruptedException {System.out.println ("- sendEmail");} public synchronized void sendSms () {System.out.println ("- sendSMS");}} / * * topic: * 1. For standard access, would you like to print email or SMS Email*/public class Lock8 {public static void main (String [] args) throws InterruptedException {Phone phone=new Phone (); / / Thread A sends Email new Thread (()-> {try {phone.sendEmail ();} catch (InterruptedExceptione) {e.printStackTrace ()) }, "A"). Start (); Thread.sleep (300); / / Thread B sends SMS newThread (()-> {phone.sendSms ();}, "B"). Start ();}}

Lock2

The 2.email method adds a 4-second pause. Would you like to print an email or a text message first? Answer: Email

Lock2 is a variant of lock1, and its principle is the same as that of lock1. Synchronized locks the same object, and the second method can not acquire the lock until the first method releases the lock.

Package day02_lam;import java.util.concurrent.TimeUnit;class Phone {public synchronized void sendEmail () throwsInterruptedException {/ / sleep 4 seconds TimeUnit.SECONDS.sleep (4); System.out.println ("- sendEmail") } public synchronized void sendSms () {System.out.println ("- sendSMS");}} / * topic: * 1. For standard access, do you want to print email first or add a 4-second pause for SMS Email*2.email method? do you want to print email first or SMS Email*/public class Lock8 {public static void main (String [] args) throws InterruptedException {/ / Resource Phone phone=new Phone () / / A sendEmail newThread (()-> {try {phone.sendEmail ();} catch (InterruptedExceptione) {e.printStackTrace ();}}, "A") .thread () / / Thread An is guaranteed to execute Thread.sleep (300); / / Thread B sendSms newThread (()-> {phone.sendSms ();}, "B"). Start ();}}

Result: sendEmail and sendSMS output at the same time after waiting for 4 seconds

Lock3

3. Add a common hello method, please print the mail or hello first? Answer: hello

Only the method of adding the synchronized keyword is controlled by the synchronized lock

Package day02_lam;import java.util.concurrent.TimeUnit;class Phone {public synchronized void sendEmail () throws InterruptedException {TimeUnit.SECONDS.sleep (4); System.out.println ("- sendEmail");} public synchronized void sendSms () {System.out.println ("- sendSMS") } public void hello () {/ / without synchronized System.out.println ("hello");}} / * * title: * 1. Standard access, I would like to ask whether to print email or SMS Email*2.email method to add a pause of 4 seconds, please print email or SMS Email*3 first. Add a new normal hello method, please print the mail first or hello hello*/public class Lock8 {public static void main (String [] args) throws InterruptedException {Phone phone=new Phone (); / / Thread A calls sendEmail new Thread (()-> {try {phone.sendEmail ();} catch (InterruptedExceptione) {e.printStackTrace ()) }, "A"). Start (); Thread.sleep (300); / / Thread B calls the normal method hello new Thread (()-> {/ / phone.sendSms (); phone.hello ();}, "B"). Start ();}}

Results: hello output first, then sendEmail output after 4s

Lock4

4. Two mobile phones, please print email or SMS first? Answer: SMS

For different instance objects, synchronized locks the corresponding calling objects.

Package day02_lam;import java.util.concurrent.TimeUnit;class Phone {public synchronized void sendEmail () throws InterruptedException {/ / sleep 4 seconds TimeUnit.SECONDS.sleep (4); System.out.println ("- sendEmail") } public synchronized void sendSms () {System.out.println ("- sendSMS");} public void hello () {System.out.println ("hello");}} / * * topic: * 1. Standard access, I would like to ask whether to print email or SMS Email*2.email method to add a pause of 4 seconds, please print email or SMS Email*3 first. Add a common hello method, please print the mail first or hellohello*4. Two mobile phones, please print email or SMS SMS*/public class Lock8 {public static void main (String [] args) throws InterruptedException {Phone phone=new Phone (); Phone phone2=new Phone () / / sychronized lock is the this corresponding to the current object Phone and phone2 respectively lock their own this / / thread A using instance phone to call sendEmail newThread (()-> {try {/ / sendEmail method contains 4-second sleep phone.sendEmail ()) } catch (InterruptedExceptione) {e.printStackTrace ();}, "A") .start (); Thread.sleep (300) / Thread B uses instance phone2 to call sendSms newThread (()-> {phone2.sendSms (); / / phone.hello ();}, "B") .start ();}}

Results: sendSMS output first, then sendEmail output after 4s

Lock5

5. Two static synchronization methods, one mobile phone, please print email or SMS first? Answer: Email

The synchronized lock static method is actually the locked class meta-information, because the static method is saved to the static area of jvm when the class meta-information is loaded, and it is the template created by all instances.

Package day02_lam;import java.util.concurrent.TimeUnit;class Phone {public static synchronized void sendEmail () throws InterruptedException {TimeUnit.SECONDS.sleep (4); System.out.println ("- sendEmail") } public static synchronized void sendSms () {System.out.println ("- sendSMS");} public void hello () {System.out.println ("hello");}} / * * topic: * 1. Standard access, I would like to ask whether to print email or SMS Email*2.email method to add a pause of 4 seconds, please print email or SMS Email*3 first. Add a common hello method, please print the mail first or hellohello*4. Two mobile phones, please print email or SMS SMS*5 first. Two static synchronization methods, one mobile phone, please print email or SMS Email*/public class Lock8 {public static void main (String [] args) throws InterruptedException {Phone phone=new Phone (); new Thread (()-> {try {phone.sendEmail ();} catch (InterruptedException e) {e.printStackTrace ()) }, "A"). Start (); Thread.sleep (300); newThread (()-> {phone.sendSms ();}, "B"). Start ();}}

Results: after 4 seconds, sendEmail was the first to output, followed by sendSMS.

Lock6

6. Two static synchronization methods, two mobile phones, please print email or SMS first? Answer: Email

Consistent with the running result of lock5, the current synchronized locks class meta-information.

Package day02_lam;import java.util.concurrent.TimeUnit;class Phone {public static synchronized void sendEmail () throws InterruptedException {TimeUnit.SECONDS.sleep (4); System.out.println ("- sendEmail");} public static synchronized void sendSms () {System.out.println ("- sendSMS") } public void hello () {System.out.println ("hello");}} / * * title: * 1. Standard access, I would like to ask whether to print email or SMS Email*2.email method to add a pause of 4 seconds, please print email or SMS Email*3 first. Add a common hello method, please print the mail first or hellohello*4. Two mobile phones, please print email or SMS SMS*5 first. Two static synchronization methods, 1 mobile phone, please print email or SMS Email*6 first. Two static synchronization methods, two mobile phones, please print email or SMS Email*/public class Lock8 {publicstaticvoidmain (String [] args) throwsInterruptedException {Phone phone=new Phone (); Phone phone2=new Phone (); new Thread ()-> {try {phone.sendEmail ();} catch (InterruptedException e) {e.printStackTrace () }, "A"). Start (); Thread.sleep (300); new Thread (()-> {/ / phone.sendSms (); phone2.sendSms (); / / phone.hello ();}, "B"). Start ();}}

Results: consistent with the running result of lock5, both sendEmail and sendSMS are output after 4s, and synchronized is also a locked class meta-information.

Lock7

7. A common synchronization method, a static synchronization method, a mobile phone, please print Email or SMS first? Answer: SMS

Principle: ordinary synchronization method. Synchronized locks the current instance object, and the current instance object exists in the heap memory area of jvm

Static synchronization method. Synchronized locks the class meta-information of the current class and exists in the static area of the jvm metaspace.

Package day02_lam;import java.util.concurrent.TimeUnit;class Phone {/ / static synchronization method public static synchronized void sendEmail () throws InterruptedException {TimeUnit.SECONDS.sleep (4); System.out.println ("- sendEmail") } / / ordinary synchronization method public synchronized void sendSms () {/ / No static System.out.println ("- sendSMS");} public void hello () {System.out.println ("hello");}} / * * topic: * 1. Standard access, I would like to ask whether to print email or SMS Email*2.email method to add a pause of 4 seconds, please print email or SMS Email*3 first. Add a common hello method, please print the mail first or hellohello*4. Two mobile phones, please print email or SMS SMS*5 first. Two static synchronization methods, 1 mobile phone, please print email or SMS Email*6 first. Two static synchronization methods, 2 mobile phones, please print email or SMS Email*7 first. A common synchronization method, a static synchronization method, a mobile phone, please print email or SMS SMS*/public class Lock8 {public static void main (String [] args) throws InterruptedException {Phonephone=newPhone (); Phonephone2=newPhone () NewThread (()-> {try {phone.sendEmail ();} catch (InterruptedExceptione) {e.printStackTrace ();}}, "A") .start () Thread.sleep; newThread (()-> {phone.sendSms (); / / phone2.sendSms (); / / phone.hello ();}, "B") .start () }}

Results: sendSMS output first, then sendEmail output after 4s

Lock8

8. A common synchronization method, a static synchronization method, 2 mobile phones, please print email or SMS first? Answer: SMS

Principle: consistent with lock7

Package day02_lam;import java.util.concurrent.TimeUnit;class Phone {public static synchronized void sendEmail () throwsInterruptedException {TimeUnit.SECONDS.sleep (3); System.out.println ("- sendEmail") } public synchronized void sendSms () {System.out.println ("- sendSMS");} public void hello () {System.out.println ("hello");}} / * * topic: * 1. Standard access, I would like to ask whether to print email or SMS Email*2.email method to add a pause of 4 seconds, please print email or SMS Email*3 first. Add a common hello method, please print the mail first or hellohello*4. Two mobile phones, please print email or SMS SMS*5 first. Two static synchronization methods, 1 mobile phone, please print email or SMS Email*6 first. Two static synchronization methods, 2 mobile phones, please print email or SMS Email*7 first. A common synchronization method, a static synchronization method, a mobile phone, please print email or SMS SMS*8 first. A common synchronization method, a static synchronization method, 2 mobile phones, please print email or SMS SMS*/public class Lock8 {publicstaticvoidmain (String [] args) throwsInterruptedException {Phone phone=new Phone (); Phone phone2=new Phone () New Thread (()-> {try {phone.sendEmail ();} catch (InterruptedExceptione) {e.printStackTrace ();}}, "A") .start () Thread.sleep; new Thread (()-> {/ / phone.sendSms (); phone2.sendSms (); / / phone.hello ();}, "B") .start () }}

Results: the running result is the same as that of lock7. SendSMS outputs first, and then sendEmail outputs after 4s.

These are the eight situations of synchronized Lock in java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report