Java在类内部创建的线程中使用此关键字

问题描述 投票:-2回答:1

我有这样的代码:

class MyClass{
    final ArrayList<Thread> list= new ArrayList<>();
    for(int i=0;i<N;i++)
        new Thread(()->{
            //more instructions...
            list.remove(this);
        }).start();

}

问题是关于Intellij显示的警告,指示list.remove(list);告诉我:ArrayList<Thread> may not contains objects of type MyClass

是Intellij的错误分析,还是我的方案中的this关键字引用了封闭的类MyClass

提前感谢

java intellij-idea this
1个回答
1
投票

[this指的是MyClass实例。

这是因为您使用的是lambda表达式,它本身不具有this上下文。

© www.soinside.com 2019 - 2024. All rights reserved.