/** * how to synchronize ending of threads * @author mh * */ class MyRun implements Runnable { // pattern of behaviour private String name; final int MAX = 10; public MyRun (String name) { this.name = name; } // constructor public void run() { // the actual code to be executed for (int i = 0; i process b.start(); try { b.join(); // Wait for this thread to end a.join(); } catch (Exception e) {} System.out.println(" -- end of ThreadDemo3 -- "); } //main } // class ThreadDemo3 class Simulate { public static void waitForSomething() { if (Math.random()<0.5) { try { Thread.sleep(200);} catch(InterruptedException e) {}; //used instead of Thread.yield() to ensure portability } // if } // waitForSomething } // class Simulate