PDA

View Full Version : [Delta] Thread scheduling



Kingdomkey01
July 1st, 2011, 10:13
Base: DeltaClean
Difficulty: 1/10

Add these two new class files

I'm not going to spoonfeed you, I'll just tell you guys what you need to add.

Tick.java


package org;

import org.Scheduler.Task;
import org.entity.NPCHandler;
import org.items.ItemHandler;
import org.model.PlayerHandler;
import org.model.packets.ShopHandler;

public class Tick extends Task implements Runnable {

public Tick(long delay) {
super(delay);
}

@Override
public void run() {
if (Server.updateServer) {
Server.calcTime();
}
PlayerHandler.getInstance().process();
NPCHandler.getInstance().process();
ItemHandler.getInstance().process();
ShopHandler.getInstance().process();
}

@Override
public Runnable getThread() {
return this;
}

}


Scheduler.java


package org;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Scheduler {

private static final ScheduledExecutorService SCHEDULER = Executors
.newSingleThreadScheduledExecutor();

public static void submit(Task task) {
SCHEDULER.scheduleWithFixedDelay(task.getThread(), 0, task.getDelay(),
TimeUnit.MILLISECONDS);
}

public static abstract class Task {
private long delay = 0;

public Task(long delay) {
this.delay = delay;
}

public long getDelay() {
return delay;
}

public Runnable getThread() {
return null;
}
}
}


Server class



Scheduler.submit(new Tick(600));



NPCHandler


private static final NPCHandler INSTANCE = new NPCHandler();

public static NPCHandler getInstance() {
return INSTANCE;
}


PlayerHandler


private static PlayerHandler instance;

public static PlayerHandler getInstance() {
if (instance == null) {
System.out.println("initialized");
instance = new PlayerHandler();
}
return instance;
}


ItemHandler


private static final ItemHandler INSTANCE = new ItemHandler();

public static ItemHandler getInstance() {
return INSTANCE;
}



ShopHandler


private static final ShopHandler INSTANCE = new ShopHandler();

public static ShopHandler getInstance() {
return INSTANCE;
}




Written by Corey - from r-s

SiniSoul
July 1st, 2011, 10:14
What happens if it takes longer than a tick cycle?

Kingdomkey01
July 1st, 2011, 10:16
What happens if it takes longer than a tick cycle?

URGH I didn't think I'd get a reply this fast, I didn't write this, I only put it on my server.

I have no idea about thread scheduling LOL

SiniSoul
July 1st, 2011, 10:18
URGH I didn't think I'd get a reply this fast, I didn't write this, I only put it on my server.

I have no idea about thread scheduling LOL

I can tell, as a suggestion and not to criticize to bad:

You cycle the updates but shorten the time frame likes so that methods that it gives more a precise tick cycle -




package net.forge.update.tasks;

import net.forge.content.entities.Player;
import net.forge.content.world.GameWorld;
import net.forge.update.impl.DelayedTask;
import net.forge.Constants;

import java.util.List;

/**
* RuneForge | 317
* UpdateControlTask.java
* @version 1.0.0
* @author SiniSoul (SiniSoul@live.com)
*/
public class UpdateControlTask extends DelayedTask {

/**
* The current stage of the update control.
*/
private int stage = 0;

@Override
public void execute() {
try {
/* Stage switch */
switch(stage) {

/* Walking Update */
case 0:
{
final List<Player> players = GameWorld.getPlayerStorage().getQuickStorage();
for(Player player : players) {
player.getWalkingQueue().process();
}
}
break;

/* Build Player Lists */
case 1:
{
final List<Player> players = GameWorld.getPlayerStorage().getQuickStorage();
for(Player player : players) {
//player.getUpdateControl().buildPlayerLists(player. getRegionMap().getPlayers(32, 32, 1));
}
}
break;

/* Write/Send Update */
case 2:
{
final List<Player> players = GameWorld.getPlayerStorage().getQuickStorage();
for(Player player : players) {
player.getUpdateControl().sendPlayerUpdate();
}
/* Reset update flags */
for(Player player : players) {
player.getUpdateFlags().reset();
}
}
break;
}
stage = (stage + 1) % (Constants.UPDATE_STAGES + 1);
} catch(Exception ex) {
ex.printStackTrace();
}
}

/**
*
*/
public UpdateControlTask() {
super((150L + (Constants.UPDATE_STAGES * 25))/Constants.UPDATE_STAGES);
}
}




Good first start though :)

The Eternity
August 19th, 2011, 00:43
What happens if it takes longer than a tick cycle?

Then the source is in need of a good rebuild.