Synchronized: Summary of thread synchronization method usage

Source: Internet
Author: User

Synchronized
1. Multi-threaded execution of the same object synchronized function, thread synchronization is correct;


The instance code is as follows:
public class Test1 implements runnable{

Defining the same object

Static TEST2 action;
 
 public static void Main (string[] args) {
  //TODO auto-generated method Stub
  
  class cls;
  try {
   cls = Class.forName ("Test2");
      action = (Test2) cls.newinstance ();
   test1 t1 = new Test1 ();
   for (int i=0;i<1;i++) {
    new Thread (T1). Start ();
    new Thread (T1). Start ();
    new Thread (T1). Start ();
    new Thread (T1). Start ();
   }
  } catch (Exception e) {
   //TODO auto-generated catch block
    E.printstacktrace ();
  }
 }

public void Run () {
TODO auto-generated Method Stub
try {
Action.getrandomstring ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

public class Test2 {

public static void Main (string[] args) {

}
Call the synchronized function of the same object without adding STATCI adornments
Public synchronized void Getrandomstring () throws Interruptedexception {
System.out.println ("$$$$$$$$$$$$$$$$$$");
Thread.Sleep (1000);
Random random = new random ();
int aa = Random.nextint (10);
int bb = Random.nextint (10);
int cc = Random.nextint (10);
System.out.println (string.valueof (aa) +string.valueof (BB) +string.valueof (cc));
System.out.println ("############");
}
}
=====================================================
=====================================================
2. Multi-threaded execution of different object synchronized function, thread synchronization error;


The instance code is as follows:
public class Test1 implements runnable{

 public static void Main (string[] args) {
  try {
   test1 t1 = new Test1 ();
   for (int i=0;i<100;i++) {
    new Thread (T1). Start ();
    new Thread (T1). Start ();
    new Thread (T1). Start ();
    new Thread (T1). Start ();
   }
  } catch (Exception e) {
   //TODO auto-generated catch block
    E.printstacktrace ();
  }
 }

public void Run () {
TODO auto-generated Method Stub
Class CLS;
try {
CLS = Class.forName ("Test2");
Test2 action = (Test2) cls.newinstance ();
Action.getrandomstring ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

public class Test2 {

public static void Main (string[] args) {

 }
 
 /**
  * @param args
  * @throws interruptedexception
  */
 // Call the synchronized function of different object, need to add statci adornment, otherwise thread synchronization fails
 public  synchronized  void getrandomstring () throws interruptedexception {
  system.out.println ("$$$$$$$$$$$$$$$$$$");
  thread.sleep (1000);
  random random = new Random ();  
        int aa = Random.nextint (10);
        int bb = random.nextint (10);
        int cc = Random.nextint (10);
  system.out.println (string.valueof (aa) +string.valueof (BB) +string.valueof (cc));
  system.out.println ("############");
 }
}
====================================================
============================================== =======
   3. Multithreaded execution of synchronized static functions of different objects, thread synchronization is correct;


The instance code is as follows:
public class Test1 implements runnable{

public static void Main (string[] args) {
try {
Test1 T1 = new Test1 ();
for (int i=0;i<100;i++) {
New Thread (T1). Start ();
New Thread (T1). Start ();
New Thread (T1). Start ();
New Thread (T1). Start ();
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

public void Run () {
TODO auto-generated Method Stub
Class CLS;
try {
CLS = Class.forName ("Test2");
Test2 action = (Test2) cls.newinstance ();
Action.getrandomstring ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

public class Test2 {

public static void Main (string[] args) {

}

/**
* @param args
* @throws interruptedexception
*/
Call the synchronized function of different object, need to add statci adornment, otherwise thread synchronization fails
Public synchronized static void Getrandomstring () throws Interruptedexception {
System.out.println ("$$$$$$$$$$$$$$$$$$");
Thread.Sleep (1000);
Random random = new random ();
int aa = Random.nextint (10);
int bb = Random.nextint (10);
int cc = Random.nextint (10);
System.out.println (string.valueof (aa) +string.valueof (BB) +string.valueof (cc));
System.out.println ("############");
}
}

=========================================================
==========================================================

Synchronized: Summary of thread synchronization method usage

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.