如何在java中同步静态方法 [英] How to synchronize static method in java

查看:203
本文介绍了如何在java中同步静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中实现单例模式时,我想出了这个问题。即使下面的例子不是我的真正的代码,但是非常相似的原始代码。

  public class ConnectionFactory {
private static ConnectionFactory instance;

public static synchronized ConnectionFactory getInstance(){
if(instance == null){
instance = new ConnectionFactory();
}

return instance;
}

private ConnectionFactory(){
//私人构造函数实现
}
}
pre>

因为我不太清楚静态同步方法的行为,我从google的一些建议 - 没有(或尽可能少)多个静态同步方法在同一个类中。我想在实现静态同步方法时,锁属于Class对象,因此多个静态同步方法可能会降低系统性能。



我是对吗?还是JVM使用其他机制实现静态同步方法?如果我在类中实现多个静态同步方法,最好的做法是什么?



谢谢大家!



解决方案

最好的方法是尽可能少地修改你的代码。

  public class ConnectionFactory {
private static ConnectionFactory instance = new ConnectionFactory();

public static ConnectionFactory getInstance(){
return instance;
}

private ConnectionFactory(){
}
}

如你所见,现在并不需要 getInstance 方法,因此您可以将代码简化为:

  public class ConnectionFactory {
public static final ConnectionFactory INSTANCE = new ConnectionFactory();

private ConnectionFactory(){
}
}

UPD关于同步:最好的方法是同步在外部类不可见的锁,即:

  public class ConnectionFactory {
private static final Object lock = new Object();

public static void doSmth(){
synchronized(lock){

...
}
}

public static void doSmthElse(){
synchronized(lock){

...
}
}
}

有很多关于为什么在想法(例如这一个),我认为在类上同步是实际的。


I come up with this question when implementing singleton pattern in Java. Even though the example listed below is not my real code, yet very similar to the original one.

public class ConnectionFactory{
    private static ConnectionFactory instance;

    public static synchronized ConnectionFactory getInstance(){
        if( instance == null ){
            instance = new ConnectionFactory();
        }

        return instance;
    }

    private ConnectionFactory(){
        // private constructor implementation
    }
}

Because I'm not quite sure about the behavior of a static synchronized method, I get some suggestion from google -- do not have (or as less as possible) multiple static synchronized methods in the same class. I guess when implementing static synchronized method, a lock belongs to Class object is used so that multiple static synchronized methods may degrade performance of the system.

Am I right? or JVM use other mechanism to implement static synchronized method? What's the best practice if I have to implement multiple static synchronized methods in a class?

Thank you all!

Kind regards!

解决方案

The best approach (which makes as few changes in your code as possible) is to do like this:

public class ConnectionFactory{
    private static ConnectionFactory instance = new ConnectionFactory();

    public static ConnectionFactory getInstance(){
        return instance;
    }

    private ConnectionFactory(){
    }
}

As you can see, there is no real need in getInstance method now, so you can simplify the code to:

public class ConnectionFactory{
    public static final ConnectionFactory INSTANCE = new ConnectionFactory();

    private ConnectionFactory(){
    }
}

UPD about synchronization: the best way is synchronizing on a lock which is not visible to outer classes, i.e.:

public class ConnectionFactory{
    private static final Object lock = new Object();

    public static void doSmth() {
        synchronized (lock) {

           ...
        }
    }

    public static void doSmthElse() {
        synchronized (lock) {

           ...
        }
    }
}

There are many discussions about "why synchronizing on this is a bad idea" (like this one), I think that the same is actual for synchronizing on class.

这篇关于如何在java中同步静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆