Interview Q: Java Synchronization

I came across these questions a couple of months ago during a Skype poll for a German company. Given the following code:

private static DateFormat DATE_FORMAT = new SimpleDateFormat();       
public void doSomething() {
    for (int i = 0; i < 100; i++) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (DATE_FORMAT) {
                    System.out.println(DATE_FORMAT.format(Calendar.getInstance().getTime()));
                }

            }
        }).start();
    }
}

Indicate if there could be any synchronization problems and why.

My intuition tells me that they should not be. We create 100 threads, each of which will capture a lock on the same object (DATE_FORMAT) and display the current time with more or less accuracy. However, I remember that the interviewer mentioned something about the discrepancies in the press, but I cannot remember correctly.

Thanks in advance.

+4
source share
3 answers

, , (DATE_FORMAT), , .

, , DATE_FORMAT , , , , format SimpleDateFormat .

+1

;

DATE_FORMAT , DATE_FORMAT, 1 ( - , , ).

SimpleDateFormat.class

// 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); // modifies the calender instance

, DATE_FORMAT DATE_FORMAT ? - @Santi

, DATE_FORMAT. 2 ( DATE_FORMAT) 1 , , .

, , , .

    private static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");     
    private static ArrayList<String> listDate  = new ArrayList<>();

    public static void doSomething() 
    {
        for (int i = 0; i < 100; i++) {

            final long msCurrentDate =   i*100;

            new Thread(new Runnable() {

                public void run() {
                   synchronized (DATE_FORMAT) {
                        listDate.add(DATE_FORMAT.format(new Date(msCurrentDate)));
                        //System.out.println(DATE_FORMAT.format(Calendar.getInstance().getTime()));
                    }
                }
            }).start();
        }


        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {
                int resultSize = listDate.size();
                System.out.println("All elements' size :" + resultSize);
                resultSize = listDate.stream().distinct().collect(Collectors.toList()).size();
                System.out.println("Unique elements' size :" + resultSize);
            }
        });


    }

, . , ( 100 ) .

, ArrayList String , .

:

enter image description here

2 ,

, 5 , .

:

//Output of all executions
//All elements' size :100
//Unique elements' size :100

:

//Output of execution : 1
//All elements' size :100
//Unique elements' size :82

//Output of execution : 2
//All elements' size :100
//Unique elements' size :78

//Output of execution : 3
//All elements' size :100
//Unique elements' size :81

, X , A, B, C... ( )

, JDK 8 api . , , - , .

+1

, new SimpleDateFormat("HH:mm:ss.SSS") , , .

, . , , .

, , Calendar.getInstance().getTime() / . , , , , , . , , , , , .

:

public class Test {
    private static DateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS");
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Date time = Calendar.getInstance().getTime();
                    synchronized (DATE_FORMAT) {
                        System.out.println(DATE_FORMAT.format(time));
                    }
                }
            }).start();
        }
    }
}
+1

Source: https://habr.com/ru/post/1649550/


All Articles