-4

Create a class named Application! Its main method (program entry point) should start two threads 0.5 seconds apart!

The result of the execution should be:

       first 0 54796(last 5 digits of the system in millisecond)
       second 0 55281
       first 1 55796
       second 1 56296
       first 2 56796
       second 2 57296
       ...
shahaf
  • 4,750
  • 2
  • 29
  • 32
  • where is your code? – Amit Oct 16 '19 at 08:39
  • Possible duplicate of [Repeat an action every 2 seconds in java](https://stackoverflow.com/questions/25296718/repeat-an-action-every-2-seconds-in-java) – ZektorH Oct 16 '19 at 08:40
  • 1
    Welcome to stackoverflow please read https://stackoverflow.com/help/how-to-ask this will explain why you should share some code and what you have tired already. please describe exactly where you are stuck. – Simulant Oct 16 '19 at 08:40
  • I tried to implement it, but no code works... – Mason Meng Oct 16 '19 at 08:44

1 Answers1

0

You can try with below approach, please don't take this code as final solution, you should try with your own approach.

class Test {

  public static void main(String[] args) throws InterruptedException {
    Thread thread1 = new Thread(() -> printEveryHalfSecond("first"));
    Thread thread2 = new Thread(() -> printEveryHalfSecond("second"));

    thread1.start();
    thread2.start();
  }

  public void printEveryHalfSecond(String prefix) {
    for (int i = 0; i < 10; i++) {
      String time = String.valueOf(System.currentTimeMillis());
      System.out.println(prefix + " " + i + " " + time.substring(time.length() - 5));
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}

Please let me know if you need any further help.

Amit
  • 1,540
  • 1
  • 15
  • 28