首页 \ 问答 \ 线程安全的枚举单例(Thread-safe Enum Singleton)

线程安全的枚举单例(Thread-safe Enum Singleton)

枚举适合创建单例。 我知道枚举方法不是线程安全的,所以我试着让它成为线程安全的。 任何人都可以确认这种实施是否正确。 使用静态和易变的这么多地方是否可以,并且可以优化? 由于内部类是私有的,因此我必须在枚举中创建函数以访问内部类功能。 可以优化吗?

import java.util.Date;

public enum SingletonWithEnum {
    INSTANCE;   

    private static class Singleton{

        private static volatile int count;
        private static volatile Date date;      

        public static  int getCount() { return count;}

        public static void setCount(int countParam) { synchronized(Singleton.class){ count = countParam; }}

        public static Date getDate() {  return date;}

        public static void setDate(Date dateParam) { synchronized(Singleton.class){ date = dateParam;}}

        public static String printObject() {
            return "Singleton [count=" + getCount() + ", date=" + getDate() + "]";
        }

    }

    public int getCount() { return Singleton.getCount();}

    public void setCount(int countParam)    {Singleton.setCount(countParam);}

    public Date getDate() { return Singleton.getDate();}

    public void setDate(Date dateParam) {Singleton.setDate(dateParam);}

    public String toString(){return Singleton.printObject();}
};

我正在使用它。

SingletonWithEnum object1 = SingletonWithEnum.INSTANCE;
object1.setCount(5);
object1.setDate(new Date());

Enums are good for creating singleton. I know enum methods are not thread-safe so I tried to make it thread-safe. Can anyone please confirm if this implementation is correct or not. Is it fine to use static and volatile so many places and can it be optimized? As inner class is private so I have to create functions in enum to access inner class functionality. Can it be optimized?

import java.util.Date;

public enum SingletonWithEnum {
    INSTANCE;   

    private static class Singleton{

        private static volatile int count;
        private static volatile Date date;      

        public static  int getCount() { return count;}

        public static void setCount(int countParam) { synchronized(Singleton.class){ count = countParam; }}

        public static Date getDate() {  return date;}

        public static void setDate(Date dateParam) { synchronized(Singleton.class){ date = dateParam;}}

        public static String printObject() {
            return "Singleton [count=" + getCount() + ", date=" + getDate() + "]";
        }

    }

    public int getCount() { return Singleton.getCount();}

    public void setCount(int countParam)    {Singleton.setCount(countParam);}

    public Date getDate() { return Singleton.getDate();}

    public void setDate(Date dateParam) {Singleton.setDate(dateParam);}

    public String toString(){return Singleton.printObject();}
};

I am using it like this.

SingletonWithEnum object1 = SingletonWithEnum.INSTANCE;
object1.setCount(5);
object1.setDate(new Date());

原文:https://stackoverflow.com/questions/28369025
更新时间:2023-09-29 06:09

最满意答案

使用.//而不是//来创建一个相对于当前上下文节点的XPath,例如:

ldteStartDate = CDate(notification.SelectSingleNode(".//StartDate").InnerText)

//从文档的根开始,因此您总是使用带有SelectSingleNode() XPath轴获得相同的第一个匹配元素。


Use .// instead of // to make an XPath that is relative to current context node, for example :

ldteStartDate = CDate(notification.SelectSingleNode(".//StartDate").InnerText)

// starts from the document's root, hence you always get the same first-matched element using such XPath axis with SelectSingleNode().

相关问答

更多
  • 对XML进行反序列化的最佳方法是创建一个类,该类对应于XML数据将反序列化的XML结构。 最新的序列化技术使用Data Contracts和DataContractSerializer 。 您使用DataMember和DataItem属性装饰我上面提到的类,并使用序列化程序进行反序列化。 The best way to deserialize XML it to create a class that corresponds to the XML structure into which the XML d ...
  • 您并不完全了解Ajax和UpdatePanel的工作方式。 实际上,UpdatePanel,从客户端,通过发出一个post请求从服务器数据询问,然后等待获取它们,之后获取数据是更新客户端。 浏览器必须发出获取数据的请求 - 服务器无法向浏览器发送任何数据,而第一个浏览器请求它们。 命令updatePanel.Update(); 有意思通知UpdatePanel有一个更新,在帖子后面的代码后面 - 并且不像您想的那样工作,不是因为您调用它而不是将数据发送到UpdatePanel。 即使您在帖子之后进行了类似的 ...
  • 由于您使用的是母版页,因此无法使用实际ID访问元素,而是访问ClientId 假设你有一个asp.net表,就像你的情况一样 在您的页面中,只需声明以下脚本即可