How do I know if a Java class or method is Java?

For example, having:

static private DateFormat df = new SimpleDateFormat(); public static void format(final Date date){ for (int i=0;i<10;i++) new Thread(new Runnable(){ public void run(){ System.out.println(df.format(date)); } }); } 

The DateFormat class is documented as an unsynchronized class, but if we use only the Method format, it cannot change the status of the hole class?

Assuming it is declared private, how do you ensure that this code is thread safe?

What is the best way to fix this code ?:

  1_ Using a different instance for every Thread. 2_ Using a synchronized block. 
+4
source share
3 answers
  • For a standard Java SE class, the best way to find out if a class is thread safe is to read its documentation carefully. Always read both class documentation and method documentation. If you say that it is not synchronized or not thread safe, you know that it is not thread safe.
  • Therefore, the DateFormat class DateFormat not thread safe. The documentation specifically states:

    Date formats are not synchronized. It is recommended that you create separate format instances for each stream. If several threads access the format at the same time, it must be synchronized from the outside.

  • Declaring a private field does not make your implementation thread safe. private simply says that external classes cannot see this field. Take a look at your method:

      for (int i=0;i<10;i++) new Thread(new Runnable(){ public void run(){ System.out.println(df.format(date)); } }); 

    The Runnable objects you create are anonymous classes. Anonymous classes are inner classes that have access to the private fields of the class surrounding them. If this is not the case, your program will not compile - they will not be able to access the df field.

    But they can. So, in fact, you have 10 threads that access your single DateFormat object referenced by df . Since we already know that DateFormat not thread safe, your program is not thread safe.

  • Also, if two external threads have references to your object (I mean an object with df inside it. You did not give a class declaration, so I don’t know what its name is). They have references to the same instance of your class. If both of them call format at the same time, both will start DateFormat.format using the same private df . Thus, it will not be thread safe.
  • To be thread safe, you need to synchronize the object or use some other type of lock (one lock for all possible threads that access it), which is what the documentation says.
  • Another way is to have a completely local object that is visible to only one thread. Not a field is a local variable that has access to a uniquely created instance of DateFormat (so you have a new copy every time you call the method). Beware of anonymous classes! In your example, even if df was a local field for the format method, it still would not be thread safe, because all your threads will access the same copy.
+3
source

According to the documents it is indicated that the format is not thread safe.

Synchronization

Date formats are not synchronized. It is recommended that you create separate format instances for each stream. If several threads access the format at the same time, it must be synchronized from the outside.

Date format

How to read it? Unless you have an explicit guarantee that a method is thread safe (or is documented as unsafe), you cannot make any assumptions that it is safe.

However, if you really want to use only one method, which may be incompatible, you can always create an environment with high concurrency and check data integrity with and without synchronization.

I had a similar question with this, with Slate and RSA. The answer there shows one way to test the specific method of the Java Java class as a whole for this. Please note, however, that the implementation may change at any time, and by making your own implementation against the details of the implementation, and then the interface may cause some unpredictable problems in the future.

data integrity testing

+3
source

I know it's hard to believe, but DateFormat.format () actually changes the state of DateFormat. For example, for SimpleDateFormat:

 // Called from Format after creating a FieldDelegate private StringBuffer format(Date date, StringBuffer toAppendTo, FieldDelegate delegate) { // Convert input date to time field list calendar.setTime(date); 

where calendar is the DateFormat field.

Therefore, I can only recommend that you trust the documentation. He may know that which he did not.

+2
source

All Articles