码迷,mamicode.com
首页 > 编程语言 > 详细

synchronized:线程同步方法使用总结

时间:2014-10-22 18:12:47      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:io   ar   使用   for   sp   on   art   cti   代码   

synchronized:
   1.多线程执行同一对象的synchronized函数,线程同步正确;


实例代码如下:
public class Test1 implements Runnable{

 //定义同一对象

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) {

 }
    //调用同一对象的synchronized 函数,无需加statci修饰
 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.多线程执行不同对象的synchronized函数,线程同步错误;


实例代码如下:
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
  */
 //调用不同对象的synchronized 函数,需要加statci修饰,否则线程同步失败
 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.多线程执行不同对象的synchronized static 函数,线程同步正确;


实例代码如下:
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
  */
 //调用不同对象的synchronized 函数,需要加statci修饰,否则线程同步失败
 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:线程同步方法使用总结

标签:io   ar   使用   for   sp   on   art   cti   代码   

原文地址:http://blog.csdn.net/lbake/article/details/40380025

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!