import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
class Scratch {
public static void main(String[] args) throws Exception {
var myTimer = new MyTimer();
var forkJoinPool = new ForkJoinPool();
forkJoinPool.execute(new RunnableTask(myTimer));
forkJoinPool.execute(new RunnableTask(myTimer));
Thread.sleep(10000);
forkJoinPool.shutdown();
}
}
class MyTimer {
private final Instant start = Instant.now();
public Duration getDuration() {
return Duration.between(start, Instant.now());
}
}
class RunnableTask implements Runnable {
private final MyTimer myTimer;
public RunnableTask(MyTimer myTimer) {
this.myTimer = myTimer;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
System.out.println(Thread.currentThread().getName() + " " + myTimer.getDuration());
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}