最佳答案
我得到这个错误作为-
无法访问 GeoLocation 类型的封闭实例。必须使用类型为 GeoLocation 的封闭实例限定分配(例如,x.new A () ,其中 x 是 GeoLocation 的实例)。这个错误出现在 新的线程任务(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);
}
}
}