Not getting expected output for threaded program

Viewed 66

Below is the program:

import java.util.Date; 

public class ThreadLocalReplaceWithString {

    public static class MyRunnable implements Runnable {

        private String threadLocal = new String("");


        @Override
        public void run() {

           threadLocal= new Date().toString();       

           try {
                        Thread.sleep(4000); 
                  } catch (InterruptedException e) { e.printStackTrace(); }

           System.out.println(Thread.currentThread().getName()+
                               " start time = "+threadLocal);

        }
    }


    public static void main(String[] args) throws InterruptedException {
        MyRunnable myRunnable = new MyRunnable();

        Thread thread1 = new Thread(myRunnable,"Thread-1");
        Thread thread2 = new Thread(myRunnable,"Thread-2");

        thread1.start();
        Thread.sleep(1000); //Start thread-2 after 1 second.
        thread2.start();


    }

}

Outputs:

Thread-1 start time = Fri May 19 06:45:47 IST 2017
Thread-2 start time = Fri May 19 06:45:47 IST 2017

Why the two time values are the same even though the new date is instantiated everytime I start a new thread. Is it because of multi core processor?

Program taken from here

2 Answers
Related