如何将可变参数传递给不同类中的Java方法?

时间:2018-12-19 17:36:42

标签: java quartz-scheduler

我知道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引用

,因此该方法不起作用

0 个答案:

没有答案