/** * MyExample.java * Example for COM1317 project * By Jing Shan **/ import java.util.Random; class MyExample implements Runnable{ int MyId=-1; Random myRand; MyExample(int i){ MyId = i; } public void run(){ System.out.println("thread "+MyId+" starts."); myRand = new Random(System.currentTimeMillis()); for(int i=0;i<10;i++){ System.out.println("id="+MyId+" : "+ i); try{ int sleepTime = myRand.nextInt(1000); System.out.println("trying to sleep "+sleepTime +" time"); Thread.sleep(sleepTime); }catch (InterruptedException e){ System.out.println("thread "+MyId+" is interrupted"); } } System.out.println("thread"+ MyId+" terminates"); } }