必须使用GeoLocation类型的封闭实例限定分配 [英] Must qualify the allocation with an enclosing instance of type GeoLocation

查看:715
本文介绍了必须使用GeoLocation类型的封闭实例限定分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误 -

无法访问GeoLocation类型的封闭实例。必须使用GeoLocation类型的封闭实例限定分配(例如xx A(),其中x是GeoLocation的实例)。此错误发生在 新的ThreadTask(i) 即可。我不知道为什么会这样。任何建议将不胜感激。

No enclosing instance of type GeoLocation is accessible. Must qualify the allocation with an enclosing instance of type GeoLocation (e.g. x.new A() where x is an instance of GeoLocation). This error is coming on new ThreadTask(i). I don't know why is it happening. Any suggestions will be appreciated.

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);
        }
    }

}


推荐答案

您好我找到了解决方案;-)

Hi I found a solution for this ;-)

发生此错误是因为您正在尝试创建内部类的实例 service.submit(new ThreadTask(i));
而不创建主类的实例..

This error happens because you're trying to create an instance of an inner class service.submit(new ThreadTask(i)); without creating instance of main class..

要解决此问题,请首先创建主类的实例:

To resolve this issue please create instance of main class first:

GeoLocation outer = new GeoLocation();

然后创建您打算调用的类实例,如下所示:

Then create instance of class you intended to call, as follows:

service.submit(outer.new ThreadTask(i));

我希望这能解决您的问题; - )

I hope this will resolve your issue;-)

这篇关于必须使用GeoLocation类型的封闭实例限定分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆