import java.util.concurrent.*;
import java.util.*;
class MyRunnable implements Runnable {
Queue<Long> queue;
MyRunnable(Queue<Long> queue) {
this.queue = queue;
}
public void run() {
try {
long start = System.currentTimeMillis();
int randomNum = ThreadLocalRandom.current().nextInt(0, 200);
Thread.sleep(randomNum);
queue.add(System.currentTimeMillis() - start);
} catch (InterruptedException e) {
System.out.println("interrupted");
queue.add((long) -1);
}
}
}
class Main {
public static void main(String[] args) throws InterruptedException {
int threadCount = 10;
Queue<Long> queue = new ConcurrentLinkedQueue<Long>();
Thread[] n = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
n[i] = new Thread(new MyRunnable(queue));
n[i].start();
}
for (int i = 0; i < threadCount; i++) {
n[i].join();
}
System.out.println(queue);
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: