【发布时间】:2012-03-16 21:17:29
【问题描述】:
我收到此错误 -
无法访问 GeoLocation 类型的封闭实例。必须使用 GeoLocation 类型的封闭实例来限定分配(例如 x.new A(),其中 x 是 GeoLocation 的一个实例)。此错误即将出现 new 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);
}
}
}
【问题讨论】:
标签: java