Executive Tasks
Aggressive handovers made easy.
Executive Iterators
Executive Iterators allow you to run a normal iterator, but attach a runnable to it, and pass the "next()" value into the runnable with each iteration.
- See ExecutiveIterator
- See ExecutiveRunnable
Here is a basic Executive Iterator
public class Example
{
public Example()
{
// Create the Executive Iterator
//We also pass in a list of players,
//We then pass in a runnable to handle all the players
GList<Player> players = Phantom.instance().onlinePlayers();
ExecutiveIterator<Player> it = new ExecutiveIterator<Player>(players)
{
public void onIterate(Player next)
{
Player p = next;
p.sendMessage("Message from an Executive");
}
};
//You can also initialize it from a GList<Player>
Phantom.instance().onlinePlayers().iterator(new ExecutiveRunnable<Player>()
{
public void run()
{
Player p = next();
p.sendMessage("Message from an Executive");
}
});
//Lets Iterate
while(it.hasNext())
{
//Executes your runnable and returns the player executed.
it.next();
}
}
}
Executive Tasks
Now, we can take an executiveIterator and run it through a special task. This task can be passed in a limit in milliseconds. Essentially, the task will run each iteration in your iterator as fast as possible, but will stop after it takes up the limit of milliseconds, and wait to the next tick to resume iterating through your task.
package org.cyberpwn.phantom.construct.test;
import org.bukkit.entity.Player;
import org.cyberpwn.phantom.Phantom;
import org.cyberpwn.phantom.sync.ExecutiveIterator;
import org.cyberpwn.phantom.sync.ExecutiveRunnable;
import org.cyberpwn.phantom.sync.ExecutiveTask;
public class Example
{
public Example()
{
//Lets make a list of players again.
ExecutiveIterator<Player> it = Phantom.instance().onlinePlayers().iterator(new ExecutiveRunnable<Player>()
{
public void run()
{
Player p = next();
p.sendMessage("Message from an Executive");
}
});
//Lets iterate it with a limit of 0.1ms (100k nanoseconds)
//We will add in the iterator and define a finish runnable
//The 0 is the interval in ticks
new ExecutiveTask<Player>(it, 0.1, 0, new Runnable()
{
@Override
public void run()
{
//The Executive Task has finished running.
//This is called when the task has finished
}
});
//You could also just tell phantom to handle it
Phantom.schedule(it);
//You can even assign it to a channel
//Warning, each channel has 1ms allocated.
//Too many channels could actually cause high ms
Phantom.schedule("your-own-channel", it);
}
}