A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/1814 below:

[java] UnsynchronizedStaticFormatter documentation and implementation wrong · Issue #1814 · pmd/pmd · GitHub

I'm not exactly sure I'm correct but I wanted to note this.

In the documentation it is written that If multiple threads must access a static formatter, the formatter must be synchronized either on method or block level.. But mustn't the formatter be synchronized on the class instead of on the instance if it is a static one? Going through the Java source code of SimpleDateFormat for example I can see that this is indeed the case. If I am correct, than the example in the documentation (and the check itself) is wrong:

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    void bar() {
        sdf.format(); // poor, no thread-safety
    }
    synchronized void foo() {
        sdf.format(); // preferred
    }
}

Rather, it should be:

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    void bar() {
        sdf.format(); // poor, no thread-safety
    }
    void foo() {
        synchronized (Foo.class) {
            sdf.format(); // preferred
        }
    }
}

Am I mistaken somewhere?


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4