Java多线程的synchronized应用

http://hi.baidu.com/chaletli/item/beb128f39dccd413d7ff8cf5

 

在使用synchronized关键字时有以下四点需要注意:

  1. synchronized关键字不能继承。

  虽然可以使用synchronized来定义方法,但synchronized并不属于方法定义的一部分,因此,synchronized关键字不能被继承。如果在父类中的某个方法使用了synchronized关键字,而在子类中覆盖了这个方法,在子类中的这个方法默认情况下并不是同步的,而必须显式地在子类的这个方法中加上synchronized关键字才可以。当然,还可以在子类方法中调用父类中相应的方法,这样虽然子类中的方法不是同步的,但子类调用了父类的同步方法,因此,子类的方法也就相当于同步了。这两种方式的例子代码如下:

  在子类方法中加上synchronized关键字


  class Parent 
  { 
  public synchronized void method() {    } 
  } 
  class Child extends Parent 
  { 
  public synchronized void method() {    } 
  }

  在子类方法中调用父类的同步方法


  class Parent 
  { 
  public synchronized void method() {    } 
  } 
  class Child extends Parent 
  { 
  public void method() { super.method();    } 
  }

  2. 在定义接口方法时不能使用synchronized关键字。

  3. 构造方法不能使用synchronized关键字,但可以使用下节要讨论的synchronized块来进行同步。

  4. synchronized可以自由放置。

  在前面的例子中使用都是将synchronized关键字放在方法的返回类型前面。但这并不是synchronized可放置唯一位置。在非静态方法中,synchronized还可以放在方法定义的最前面,在静态方法中,synchronized可以放在static的前面,代码如下:


  public synchronized void method(); 
  synchronized public void method(); 
  public static synchronized void method(); 
  public synchronized static void method(); 
  synchronized public static void method();

  但要注意,synchronized不能放在方法返回类型的后面,如下面的代码是错误的:


    public void synchronized method(); 
  public static void synchronized method();

  synchronized关键字只能用来同步方法,不能用来同步类变量,如下面的代码也是错误的。


    public synchronized int n = 0; 
  public static synchronized int n = 0;

  虽然使用synchronized关键字同步方法是最安全的同步方式,但大量使用synchronized关键字会造成不必要的资源消耗以及性能损失。虽然从表面上看synchronized锁定的是一个方法,但实际上synchronized锁定的是一个类。也就是说,如果在非静态方法method1和method2定义时都使用了synchronized,在method1未执行完之前,method2是不能执行的。静态方法和非静态方法的情况类似。但静态和非静态方法不会互相影响。看看如下的代码:


    package test; 
  public class MyThread1 extends Thread 
  { 
  public String methodName; 
  public static void method(String s) 
  { 
  System.out.println(s); 
  while (true) 
  ; 
  } 
  public synchronized void method1() 
  { 
  method("非静态的method1方法"); 
  } 
  public synchronized void method2() 
  { 
  method("非静态的method2方法"); 
  } 
  public static synchronized void method3() 
  { 
  method("静态的method3方法"); 
  } 
  public static synchronized void method4() 
  { 
  method("静态的method4方法"); 
  } 
  public void run() 
  { 
  try 
  { 
  getClass().getMethod(methodName).invoke(this); 
  } 
  catch (Exception e) 
  { 
  } 
  } 
  public static void main(String[] args) throws Exception 
  { 
  MyThread1 myThread1 = new MyThread1(); 
  for (int i = 1; i <= 4; i++) 
  { 
  myThread1.methodName = "method" + String.valueOf(i); 
  new Thread(myThread1).start(); 
  sleep(100); 
  } 
  } 
  }

  运行结果如下:

  非静态的method1方法

  静态的method3方法

  从上面的运行结果可以看出,method2和method4在method1和method3未结束之前不能运行。因此,我们可以得出一个结论,如果在类中使用synchronized关键字来定义非静态方法,那将影响这个中的所有使用synchronized关键字定义的非静态方法。如果定义的是静态方法,那么将影响类中所有使用synchronized关键字定义的静态方法。这有点象数据表中的表锁,当修改一条记录时,系统就将整个表都锁住了,因此,大量使用这种同步方式会使程序的性能大幅度下降。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值