我正在尝试创建一个在 main 方法中实现 Runnable 接口的类的对象,并将这些对象传递给线程池。但是 IDE 显示读取非静态变量的错误,这不能从静态上下文中引用,即,我一开始就无法创建对象。我无法弄清楚这段代码到底有什么问题。其他一切正常,但只有这行代码显示编译错误。有人可以帮忙吗??
package threads;
import java.util.concurrent.*;
public class Tut5 {
public static void main(String[] args) {
ExecutorService exe = Executors.newFixedThreadPool(2);
for(int i=0; i<5; i++) {
Runner5 r5 = new Runner5(i);
exe.submit(r5);
}
exe.shutdown();
System.out.println("All tasks submitted.");
try {
exe.awaitTermination(1, TimeUnit.DAYS);
}
catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("All tasks completed.");
}
class Runner5 implements Runnable {
private int id;
public Runner5(int id) {
this.id = id;
}
public void run() {
System.out.println("Starting thread: " + id);
try{
Thread.sleep(3000);
}
catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("Ending thread: " + id);
}
}
}
你去@jtahlborn