From Robowiki
Revision as of 13:57, 27 January 2010 by Duyn (talk | contribs) (New page created when I felt like editing the information on the old wiki page.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Threads can be used to offload big chunks of work without skipping turns. Robocode imposes some specific constraints on robots using threads:

  1. You can only run 5 threads (not including your main thread) at a time. Previous threads have to terminate before the security manager will let you run more.
    • This does not affect thread creation. You can create as many Thread objects as you wish, but you may only run 5 at a time.
  2. You must stop your threads at the end of each round, or you will throw an Exception (and all the nasties that entails).
    • This means you must override both onWin(...) and onDeath(...) to ensure your child threads get stopped at the end of every round, win or lose.
  3. You must also stop your threads at the end of the battle, or your robot will throw an Exception if the user stops a battle before it finishes. It's also annoying having to wait for robots to be killed when they don't do this (especially if you're debugging them!)
    • To ensure your robot also gets stopped when the user aborts a battle, you must override onBattleEnded(...) and stop your threads if they haven't stopped already.

Needless to say, threading is not for the faint of heart.

Fairness Concerns

Since threads aren't stopped during the enemy's turn, they can be used to lower the enemy's score. You could spawn a thread which does heavy work during the enemy's turn to increase the chances of them skipping a turn. Needless to say, this is not cool.

Because of these concerns, robot multithreading may not always be supported by Robocode into the future.

Sample Code

The following code spawns 5 threads (one per turn) which print a message each turn for 100 turns, then terminate.

import robocode.*;
public class ThreadDemo extends AdvancedRobot {
    // This will be used to tell our threads to terminate. To do this, we must
    // be able to modify its value, so a simple Boolean object will not do.
    // It will also be used to make our threads wait until the next round. For
    // this, it must inherit from Object to get at its notifyAll() method.
    final boolean[] token = {true};
    // Number of threads we have spawned.
    int threadCount = 0;

    public void run() {
        // Get the radar spinning so we can get ScannedRobotEvents
        while (true) {
            out.println("Robot time: " + getTime());
            if (threadCount < 5) {
                final long spawnTime = getTime();
                // Quick and dirty code to create a new thread. If you don't
                // already know how to do this, you probably haven't learned all
                // the intricacies involved in multithreading on the JVM yet.
                new Thread(new Runnable() {
                    int runCount = 0;
                    public void run() {
                        synchronized(token) {
                            while (token[0] == true && runCount < 100) {
                                    "\tHi from Thread#%d (current time: %d). Repeat count: %d\n",
                                    spawnTime, getTime(), ++runCount);
                                    // Sleep until re-awakened in next turn
                                } catch (InterruptedException e) {}

    public void onScannedRobot(ScannedRobotEvent event) {
        synchronized(token) {
            // Wake up threads! It's a new turn!

    // The following events MUST be overriden if you plan to multithread your robot.
    // Failure to do so can cause exceptions and general annoyance.

    public void onWin(WinEvent e) {
        // Politely tell our threads to stop because the round is over
        synchronized(token) { token[0] = false; }

    public void onDeath(DeathEvent e) {
        // Politely tell our threads to stop because the round is over
        synchronized(token) { token[0] = false; }

    public void onBattleEnded(BattleEndedEvent e) {
        // Politely tell our threads to stop because the battle has been ended.
        // This gets called whether the battle was aborted or ended naturally,
        // so beware of duplication with onDeath/onWin (if that is important to you).
        synchronized(token) { token[0] = false; }

Related Links