0

I've got class A which i need for getting the current date and modifying it.

public class A {

    private Calendar cal = Calendar.getInstance();

    public void change() {
        try
        {
            cal.add(Calendar.MONTH, 1);
            Thread.sleep(1000);
        }
        catch (Exception e)
        {
            System.out.println("Thread  interrupted.");

        }


    }
    public void print() {
       DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

       System.out.println("Current Date Time : " + dateFormat.format(cal.getTime()));
    }
}

In my main function, i created an instance of class A and also created two threads : one responsible for changing the date and the other for printing it to the console(using print and change mathods of A)

What I want it to do is to print the new date after each change, however I got the same date being printed.

public class Main {

    public static void main(String[] args) throws InterruptedException {
        A a = new A();
        Thread B = new Thread(new Runnable(){
            public void run() {
                for (int i = 0; i < 10; i++) {
                    a.print();
                }
            }

        });

        Thread C = new Thread(new Runnable(){
            public void run(){
                for(int i =0; i<10; i++) {
                    a.change();
                }

            }
        });
        synchronized (a) {
                B.start();
                C.start();

        }
    }
}

and the output is like this:

Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
Current Date Time : 2019/12/29 18:27:27
GJL
  • 143
  • 8
  • 1
    so what happened here is the thread B ran first before C ever got started. There is no logic here to make these 2 threads interleave neatly like your title says is desired. You could take a look at https://stackoverflow.com/questions/16689449/printing-even-and-odd-using-two-threads-in-java to see how people do this kind of thing. – Nathan Hughes Nov 29 '19 at 17:44
  • I recommend you don’t use `SimpleDateFormat` and `Calendar`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 04 '19 at 15:41

1 Answers1

0

Here is the updated version of your code, which will work fine for you:

class A {

private BlockingQueue<Calendar> calList = new ArrayBlockingQueue<Calendar>(1);

public void change() {
    try {
        Calendar cal=Calendar.getInstance();
        while(true) {
            cal.add(Calendar.MONTH, 1);
            calList.add(cal);
            System.out.println("Adding month to date");
            Thread.sleep(1000);
        }
    } catch (Exception e) {
        System.out.println("Thread  interrupted.");

    }

}

public void print() throws InterruptedException {
    while(true) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println("Current Date Time : " + dateFormat.format(calList.take().getTime()));
    }
}}

Currently, it keeps on looping and adds the month. You can change it as required:

public static void main(String[] args) throws InterruptedException {
    A a = new A();
    Thread B = new Thread(new Runnable() {
        public void run() {
            try {
                a.print();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    Thread C = new Thread(new Runnable() {
        public void run() {
            a.change();
        }
    });
    B.start();
    C.start();

}
Prashant K
  • 869
  • 9
  • 9