Hello. Today I removed my old Hyperion Game engine because I had some issues with it. I added Grahams Task Scheduler instead.
But I have a question!
Since I made the main server processes run using the Task sheduler, i removed the catch exception in the main method. So it looked like this:
Code:
scheduler.schedule(new Task() {
@Override
protected void execute() {
itemHandler.process();
playerHandler.process();
npcHandler.process();
shopHandler.process();
CycleEventHandler.getSingleton().process();
objectManager.process();
fightPits.process();
CastleWars.process();
pJClans.process();
PublicEvent.process();
miniGames();
}
});
But when I use this, the server never prints out Debug. So I added the Debug to the task from my old PI, so it looks like this:
Code:
scheduler.schedule(new Task() {
@Override
protected void execute() {
try {
if (sleepTime > 0)
Thread.sleep(sleepTime);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("A fatal exception has been thrown!");
for(Player p : PlayerHandler.players) {
if(p == null)
continue;
PlayerSave.saveGame((Client)p);
System.out.println("Saved game for " + p.playerName + ".");
}
}
engineTimer.reset();
itemHandler.process();
playerHandler.process();
npcHandler.process();
shopHandler.process();
CycleEventHandler.getSingleton().process();
objectManager.process();
fightPits.process();
CastleWars.process();
pJClans.process();
PublicEvent.process();
miniGames();
cycleTime = engineTimer.elapsed();
if(cycleTime < 575)
sleepTime = cycleRate - cycleTime;
else
sleepTime = 0;
cycleTime = engineTimer.elapsed();
sleepTime = cycleRate - cycleTime;
totalCycleTime += cycleTime;
cycles++;
debug();
}
});
And it works perfectly. It now prints out debug.
But I have a very important question. I had to add a catch since there's a Thread that's set to sleep in the task, have I done it wrong?
Look at the red part.
Should the catch be added before the task?
Or should it be within the task, but catch everything? Like:
Code:
scheduler.schedule(new Task() {
@Override
protected void execute() {
try {
if (sleepTime > 0)
Thread.sleep(sleepTime);
engineTimer.reset();
itemHandler.process();
playerHandler.process();
npcHandler.process();
shopHandler.process();
CycleEventHandler.getSingleton().process();
objectManager.process();
fightPits.process();
CastleWars.process();
pJClans.process();
PublicEvent.process();
miniGames();
cycleTime = engineTimer.elapsed();
if(cycleTime < 575)
sleepTime = cycleRate - cycleTime;
else
sleepTime = 0;
cycleTime = engineTimer.elapsed();
sleepTime = cycleRate - cycleTime;
totalCycleTime += cycleTime;
cycles++;
debug();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("A fatal exception has been thrown!");
for(Player p : PlayerHandler.players) {
if(p == null)
continue;
PlayerSave.saveGame((Client)p);
System.out.println("Saved game for " + p.playerName + ".");
}
}
}
});
Or is it fine the way I've done it? I'm fairly bad with threads and I know that they can cause deadlocks that's why I'm asking someone with some knowledge if it's done correctly.
Thanks!