/** Runnable that prints out forever the elements of the sequence seed, seed+2, seed+4, .. and sleeps a random time in between each element */ class ChildR implements Runnable { private int seed; public ChildR(int seed) { this.seed = seed; } public void run() { for (int k = seed; ; k += 2) { System.out.println(k); try { Thread.sleep((long)(1000*seed*Math.random())); } catch (InterruptedException e) { System.out.println("Interrupted sleep"); break; } } } } /** It starts two ChildR threads with different seeds and waits for them to terminate (they do not) */ public class Thread1R { public static void main(String[] args) { Thread child1 = new Thread(new ChildR(1)); child1.start(); Thread child2 = new Thread(new ChildR(2)); child2.start(); try { Thread.sleep(10000); // sleep 10 seconds child1.interrupt(); child2.interrupt(); child1.join(); child2.join(); } catch (InterruptedException e) { System.out.println("Interrupted join"); } } }