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.