在下面的代码中,我想知道为什么内联类中的 run() 能够访问外部类变量 - 信号量(即使它没有声明为 final)。
private Semaphore semaphore = new Semaphore(bufferSize);
private Runnable producerRunnable = new Runnable(){
@Override
public void run()
{
try
{
semaphore.acquire();
}
catch(InterruptedException e)
{
System.out.println("producer was interrupted while trying to acquire semaphore");
}
}
};
我希望 Eclipse 标记编译错误 - 因为信号量引用未声明为 final