Like so?
abstract public class BaseTask extends Runnable {
public BaseTask(ParamA aParam) {
// do something with aParam
StaticExecutorService.submit(this);
}
}
public class AbcTask extends BaseTask {
public ABC(ParamA aParam, ParamB bParam) {
super(aParam);
}
@Override
public void run() {
}
}
The class can then just be created and queued for execution by doing a
new AbcTask();
-
I’m not sure if this is ok because maybe it is still being constructed when
ExecutorServicedecides to executeAbcTask? -
If you don’t recommend this for whatever reason, please state and elaborate.
Thanks
In your example it will probably work but as the call to
super()has to be the first in child classes ofBaseTaskthis means that the constructor won’t be able to perform any operation before the submission of the task.From you sample, the second parameter cannot be used since the task will be submitted before you can assign it to an attribute.
If we consider the following code:
Since
run()can be called before the assignment ofthis.nametheSystem.outline can end inNullPointerException