I'm working on a little system of taxes for some kingdoms in game. So I have a function such as "Kingdom ruler can reset tax rate's an interval for it's gaining".
Image of console log
To avoid data replacement when the task is completed with new data. We just reset old model by new. And next schsedule will invoke by new
public synchronized void startSystem(float rate, int interval) {
rate = rate < 0
? ETaxState.GOOD.getValue() : rate > ETaxState.INSANE.getValue()
? ETaxState.INSANE.getValue() : rate;
interval = interval < 1
? MIN_INTERVAL : interval > MAX_INTERVAL
? MAX_INTERVAL : interval;
taxModel = new TaxesModel(rate, interval);
task = ThreadPool.scheduleAtFixedRate(this, 1_000, interval * 3_600_600);
}
This method stop the current task and reset the model for a future task with new tax rates and interval of income get.
public synchronized void stopSystem() {
if (task != null) {
task.cancel(false);
task = null;
}
taxModel = null;
}
Finally I have a some instance for invoke this methods. Firstly we stop the current task, and after get thecorrect numbers we set a new task -> launch.
try {
myKingdom.getTaxes().stopSystem();
StringTokenizer st = new StringTokenizer(command);
st.nextToken(); //go throught command
float newTaxRate = Float.parseFloat(st.nextToken());
int newInterval = Integer.parseInt(st.nextToken());
myKingdom.getTaxes().startSystem(newTaxRate, newInterval);
} catch (NumberFormatException e) {
log.error("Can't change task", e);
}
Im afraid the task.cancel(false)
becuase javadoc says, that task will be done and after will be canceled. If I set the true value inside cancel, I get the immediently interrupt of task, but I do not know what it will lead to... And how to be?
In final game example it must looks like that: When player reset the new values of task, the CURRENT task must completed with old tax rates, or I get the bugs...
Thank you for help! Sorry for my bad english!