-------------------转自CSDN lonely_fireworks 的博客-----------------------
synchronized关键字有两种用法,一种是只用于方法的定义中,另外一种是synchronized块,我们不仅可以使用synchronized来同步一个对象变量,你也可以通synchronizedl来同步类中的静态方法和非静态方法。
synchronized块的语法如下:
- public void method()
- {
- synchronized(表达式)
- {
- }
- }
public void method()
{
synchronized(表达式)
{
}
}
第一种:非静态方法的同步
从java相关语法可以知道使用synchronized关键字来定义方法就会锁定类中所用使用synchroniezd关键字定义的静态方法和非静态方法,但是这有点不好理解,如果要synchronized块,来达到这样的效果,就不难理解为什么会产生这种效果了,如果使用synchronized来锁定类中所有的同步非静态方法,只需要使用this作为synchronized块的参数传入synchronized块中,代码如下:
通过synchronized块来同步非静态方法
在上面的代码中的method1使用了synchronized块,method2方法是用了synchronized关键字来定义方法,如果使用同一个Test实例时,这两个方法只要有一个在执行,其他的方法都会因未获得同步锁而被堵塞。除了使用this作为synchronized块的参数,也可以使用Test.this作为synchronized块的参数来达到同样的效果。
- public class Test
- {
- public void method1()
- {
- synchronized(this)
- {
- }
- }
- public synchronized void method2()
- {
- }
- }
public class Test
{
public void method1()
{
synchronized(this)
{
}
}
public synchronized void method2()
{
}
}
在内类中使用synchronized块中,this只表示内类,和外类(OuterClass)没有关系。但是内类中的非静态方法和外类的非静态方法也可以同步。如果在内类中加个方法method3也可以使和Test里面的2个方法同步,代码如下:
- public class Test
- {
- class InnerClass
- {
- public void method3()
- {
- synchronized(Test.this){
- }
- }
- }
- }
public class Test
{
class InnerClass
{
public void method3()
{
synchronized(Test.this){
}
}
}
}
上面InnerClass的method3方法与Test的method1和method2方法在同一时间内只能有一个方法执行。
synchronized块不管是正确执行完,还是因为程序出错因异常退出synchronized块,当前的synchronized块所持有的同步锁都会自动释放,因此在使用synchronized块不必担心同步锁的问题。
二、静态方法的同步
由于在调用静态方法时,对象实例不一定被创建,因此,就不能使用this来同步静态方法,而必须使用Class对象来同步静态方法。代码如下:
- public class Test{
- pubic static void method1(){
- synchronized(Test.class){
- }
- }
- public static synchronized void method2(){
- }
- }
public class Test{
pubic static void method1(){
synchronized(Test.class){
}
}
public static synchronized void method2(){
}
}
在同步静态方法时可以使用类的静态字段class来得到class对象,在上例中method1和method2方法只有一个方法执行,除了使用class字段可以得到class对象,还可以通过实例的getClass()方法获取class对象,代码如下:
- public class Test{
- public static Test test;
- public Test(){
- test=this;
- }
- public static void method1(){
- synchronized(test.getClass()){
- }
- }
- }
public class Test{
public static Test test;
public Test(){
test=this;
}
public static void method1(){
synchronized(test.getClass()){
}
}
}
在上面的代码中,我们通过一个public的静态对象得到Test的一个实例,并通过这个实例的getClass方法获取一个class对象(注意一个类的所有实例通过getClass方法得到的都是同一个Class对象)。我们也可以通过class使不同类的静态方法同步,代码如下:
Test类中的方法和Test1类中方法同步。
- public class Test1{
- public static void method1(){
- synchronized(Test.class){
- }
- }
- }
public class Test1{
public static void method1(){
synchronized(Test.class){
}
}
}
注意:在使用synchronized块来同步方法时,非静态方法可以通过this来同步,而静态方法必须使用class对象来同步,但是非静态方法也可以通过使用class来同步静态方法。但是静态方法中不能使用this来同步非静态方法。这点在使用synchronized块需要注意。