The best way to solve this problem is to keep a list of enemies that exist on the screen, every time you make the next screen, your main rendering cycle should determine the weather, it should call any of the methods of the Enemy object or not.
public interface Enemy { public void doNextThing(); } public class TimedEnemy implements Enemy { private long lastExecute; private Enemy enemy; private long threshHold; public TimedEnemy(Enemy enemy, long threshold) { this.lastExecute = System.currentTimeMills(); this.enemy = enemy; this.threshold = threshold; } public void doNextThing() { long duration = System.currentTimeMills() - lastExecute; if( duration >= threshold) { lastExecute = System.currentTimeMills(); this.enemy.doNextThing(); } } }
If you really need each enemy AI to start in its own thread, you need to use the TaskExecutor functions for Java 5 with the concept of Futures. Although running each AI on separate threads means you have to be careful with thread synchronization.
source share