Thread-safe dynamic pattern for NumberFormat / DecimalFormat
<p>Haven't been able to find appropriate solution in web, therefore I thought to ask if my way of using a java format is correct. </p>

<p>1) In the NumberFormat.java documentation it says that </p>

<blockquote>
  <p>Number formats are generally not synchronized. It is recommended to create separate format instances for each thread.</p>
</blockquote>

<p>We have been using format objects (statically initialized) in a multi-threaded environment with no issues so far. Is it maybe because once the formats are defined, we their state is not changed (ie, no setters are called afterwards)</p>

<p>2) I now need to define a new format which should output either one or two significant digits after comma, depending on some extra logic. The way i did it was to define a new format wrapper and delegate to two distinct DecimalFormat depending on the case in the overwritten #format(double, StringBuffer, FieldPosition) method. Here is the code for that:</p>

<pre><code>private final NumberFormat FORMAT = new DecimalFormat() {
    private final NumberFormat DECIMAL_FORMAT = new DecimalFormat("0.##");
    private final NumberFormat DECIMAL_FORMAT_DIGIT = new DecimalFormat(
                    "0.0#");
    public StringBuffer format(double number, StringBuffer result, java.text.FieldPosition fieldPosition) {
        if ((number &gt;= 10 &amp;&amp; Math.ceil(number) == number)) {
            return DECIMAL_FORMAT.format(number, result, fieldPosition);
        } else {
            return DECIMAL_FORMAT_DIGIT.format(number, result, fieldPosition);
        }
    }
};
</code></pre>

<p>Is it the best practice? I have concerns about not actually using the wrapper class (it serves only to comply with the NumberFormat interface and delegates all the work on inner formats). I do not want to call DecimalFormat#applyPattern() as i think this would compromize the volatile concurrency. </p>

<p>Thanks</p>