- Java code
public class ThreadTest{ public static void main(String[] args){ new ThreadTest().start(); } public void start(){ ThreadNew tn = new ThreadNew(); Thread tn1 = new Thread(tn); Thread tn2 = new Thread(tn); Thread tn3 = new Thread(tn); tn1.start(); tn2.start(); tn3.start(); } } class ThreadNew implements Runnable{ public void run(){ for(int i = 0;i <10000;i++){ System.out.println(" CURRENT:" + Thread.currentThread().getName()); sleep(1000); } } }
------解决方案--------------------
因为Runnable接口只有run()方法,Thread类才有sleep这个静态方法。
楼主的代码要么改为继承自Thread类,在sleep(1000)那里加上try catch
要么改为Thread.sleep(1000),然后加上try catch,建议使用Thread.sleep(1000)
因为sleep是静态方法,不建议使用对象调用。