我知道Java是“按值传递”。但是我如何实现通过参考的目标呢?
ClassA包含一个易失性变量
public class ClassA extends QuartzJobBean implements InterruptableJob {
private volatile boolean isInterrupted = false;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext)
throws JobExecutionException {
new ClassB().callNewMethod(isInterrupted);
//I want callNewMethod get the REFERENCE and not the VALUE of isInterrupted
}
@Override
public void interrupt() throws UnableToInterruptJobException {
isInterrupted = true;
}
}
ClassB包含辅助方法
public class ClassB {
protected void callNewMethod(boolean isInterrupted) {
stepOne();
if (!isInterrupted) {
stepTwo();
}
if (!isInterrupted) {
stepThree();
}
if (!isInterrupted) {
stepFour();
}
}
}
我尝试了同步,但是由于每个线程都需要拥有自己的易失isInterrupted
引用