Java Multi-thread synchronized (v)

Source: Internet
Author: User

The previous article introduced the use of synchronized modified static way to achieve "Class lock", today to introduce another way to implement, synchronized (Class) code block, the wording is different but the function is the same. Here I enclose a piece of code to look at the basic usage of the synchronized (class) code block, as follows:

public static void Main (string[] args) {Service4S1= new Service4 (); Service4S2= new Service4 (); Threada A = newThreada (S1); THREADB B = Newthreadb (S2); A.setname ("a"); B.setname ("B"); A.start (); B.start ();} public static class Threada extends Thread {private Service4 service;public Threada (Service4 service) {super (); This.servi CE = service;} @Overridepublic void Run () {super.run ();Service.printa ();}}public Static class Threadb extends Thread {private Service4 service;public threadb (Service4 service) {super (); This.ser vice = service;} @Overridepublic void Run () {super.run ();service.printb ();}}}class Service4 {Staticpublic voidPrintA (){synchronized (service4.class){try {System.out.println ("thread:" + thread.currentthread (). GetName () + "in" + system.currenttimemillis () + "Enter Printa"); Thread.Sleep (3000); System.out.println ("Thread:" + thread.currentthread (). GetName () + "in" + system.currenttimemillis () + "Leave Printa"); catch (Interruptedexception e) {e.printstacktrace ();}}}Staticpublic voidPRINTB (){synchronized (service4.class){System.out.println ("thread:" + thread.currentthread (). GetName () + "in" + system.currenttimemillis () + "Enter Printb"); try { Thread.Sleep (3000);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread:" + thread.currentthread (). GetName () + "in" + system.currenttimemillis () + "Leave Printb");}}

The results of the operation are as follows: The synchronized (class) code block acts the same as synchronized static.

As I said before, synchronized can also pass in other instance objects or parameters of a method, so I'm going to talk about a special case of using synchronized (class) with string, or a code explanation, and here's a piece of code that reads:
public static void Main (string[] args) {Service5 service = new Service5 (); Threada a = new Threada (service), A.setname ("a"); A.start (); THREADB B = new threadb (service), B.setname ("B"); B.start ();} public static class Threadb extends Thread {private Service5 service;public threadb (Service5 service) {super (); This.servi CE = service;} @Overridepublic void Run () {//TODO auto-generated method Stubsuper.run ();service.print ("AA");}}public Static class Threada extends Thread {private Service5 service;public Threada (Service5 service) {super (); This.ser vice = service;} @Overridepublic void Run () {//TODO auto-generated method Stubsuper.run ();service.print ("AA");}}}class Service5 {publicStaticvoidprint (string string){try {synchronized (String){while (true) {System.out.println (Thread.CurrentThread (). GetName ()); Thread.Sleep (1000);}} catch (Interruptedexception e) {e.printstacktrace ();}}}
   The results of the operation are as follows: You can see that the output will print out countless consecutive a, because there is a string constant pool cache function in the JVM, so the "AA" in Printa () and PRINTB () Two methods is the same value, so two threads hold the same lock, So there is always one thread that does not execute.

the above has seen the problem of constant pool, so in most cases, the string is not the most like lock, but instead of the other, such as new object () instantiation of an object. You can look at the following example, the difference after the run, as follows:

public static void Main (string[] args) {Service6 service = new Service6 (); Threada a = new Threada (service), A.setname ("a"); A.start (); THREADB B = new threadb (service), B.setname ("B"); B.start ();} public static class Threadb extends Thread {private Service6 service;public threadb (Service6 service) {super (); This.servi CE = service;} @Overridepublic void Run () {//TODO auto-generated method Stubsuper.run ();Service.print (New Object ());}}public Static class Threada extends Thread {private Service6 service;public Threada (Service6 service) {super (); This.ser vice = service;} @Overridepublic void Run () {//TODO auto-generated method Stubsuper.run ();Service.print (New Object ());}}}class Service6 {public static void print (Object Object) {try {synchronized (Object) {while (true) {System.out.println (Thread.CurrentThread (). GetName ()); Thread.Sleep (1000);}} catch (Interruptedexception e) {e.printstacktrace ();}}}

The results of the operation are as follows: You can see that the result of the run is cross-asynchronous, indicating that two threads hold a lock that is not the same lock.

Java Multi-thread synchronized (v)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.