以下程序运行报错 ,MyRun 的sleep 如何指定是那个 Thread 的。
import java.util.*;
public class Testinterrupt
{
public static void main(String [] args)
{
MyRun r = new MyRun();
Thread t = new Thread(r);
t.start();
try
{
t.sleep(10000);
}
catch(InterruptedException e)
{
}
t.interrupt();
}
}class MyRun implements Runnable
{
public void run()
{
while(true)
{
System.out.println("====="+new Date()+"========"); try
{
sleep(1000);
}
catch(InterruptedException e)
{
return;
}
}
}
}
import java.util.*;
public class Testinterrupt
{
public static void main(String [] args)
{
MyRun r = new MyRun();
Thread t = new Thread(r);
t.start();
try
{
t.sleep(10000);
}
catch(InterruptedException e)
{
}
t.interrupt();
}
}class MyRun implements Runnable
{
public void run()
{
while(true)
{
System.out.println("====="+new Date()+"========"); try
{
sleep(1000);
}
catch(InterruptedException e)
{
return;
}
}
}
}
t.sleep(10000); 这一句不用这写,直接换成Thread.sleep()即可。就表示对当前线程休眠。报的错是MyRun类run方法的
sleep(1000);
这句吗?换成Thread.sleep()