编写三个线程分别显示各自的运行时间,第一个线程每隔1秒钟运行一次,第二个线程每隔5秒钟运行一次,第三个线程每隔10秒钟运行一次。
源代码
线程类Sleep类
package experiment.exp8;
public class Sleep extends Thread{
public int SleepTime=0;
public long time=0;
public int i=0;
public int k=1;
public Sleep(String s) {
super(s);
}
public synchronized void run()
{
long start=System.currentTimeMillis();
while(i==0)
{
System.out.println(Thread.currentThread().getName()+"正在运行第"+k+++"次");
try
{
sleep(SleepTime);
}
catch(InterruptedException e)
{
e.getStackTrace();
}
long end=System.currentTimeMillis();
time=time+end-start;
}
}
}