我得到这个错误as-
不能访问GeoLocation类型的封闭实例。必须使用类型GeoLocation的封闭实例(例如x.new A(),其中x是GeoLocation的实例)限定分配。这个错误是在新的ThreadTask(i)。我不知道为什么会发生。任何建议将不胜感激。
public class GeoLocation {
public static void main(String[] args) throws InterruptedException {
int size = 10;
// create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(size);
// queue some tasks
for(int i = 0; i < 3 * size; i++) {
service.submit(new ThreadTask(i));
}
// wait for termination
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
}
class ThreadTask implements Runnable {
private int id;
public ThreadTask(int id) {
this.id = id;
}
public void run() {
System.out.println("I am task " + id);
}
}
}