English 中文(简体)
启动静态成员单一吨
原标题:Singleton with initializing a static member

在我最初设计的后面的《法典》中,“批号”需要在整个申请执行过程中发出下一个增益值。 因此,我把这个班子变成一个单一州。 然而,随着最近一些要求的变化,我需要重新确定“批号”。 我只是添加了这样做的重新定点方法。 然而,这无疑违反了单一州的模式,我也知道,以这种方式启动固定成员并不是一个好的想法。

你认为我应该做些什么?

public final class GetNextNumber {
    private static GetNextNumber instance; 
    private static Integer nextNumber=1;
    private GetNextNumber() {
    }
    public static synchronized GetNextNumber getInstance() {
        if(instance==null){
            instance = new GetNextNumber();
        }
        return instance;
    } 
    protected Integer getNextNumber(){
        return nextNumber++;
    }
    protected synchronized void reset(){
        nextNumber=1;
    }
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
}
最佳回答

为什么没有田地就是一个例子变量? 这里无需静态。

重新设定的一定要同步进行,除非获得NextNumber。

问题回答

看一下科索沃,除两点外:

  • getNextNumber is not synchronized.
  • since getNextNumber and reset are not static, nextNumber doesn t need to be static, either.

您可使用rel=“nofollow”>AtomicInteger>,以避免出现reset >:code>

public final class GetNextNumber {

    private static GetNextNumber instance;

    private AtomicInteger nextNumber = new AtomicInteger(1);

    private GetNextNumber() {
    }

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

    protected Integer getNextNumber(){
        return nextNumber.getAndIncrement();
    }

    protected void reset(){
        nextNumber.set(1);
    }
}

关于这方面的进一步讨论,见http://www.javamex.com/tutorials/synchronization_concurrency_7_atomic_integer_long.shtml”rel=“nofollow>。 Java5级原子学:原子学和原子学::

Before Java 5, we had to write classes with access to the counter variable in synchronized blocks or methods, or else use a volatile variable which is a lighter form of synchronization but with the risk that some updates could be missed if they happen concurrently. An AtomicInteger can be used as a drop-in replacement that provides the best of both worlds...





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签