I have this code that tick my JAVA game, but I need to create a new thread for each entity in the iterator.
public void tick(){
player.tick();
Iterator<Entity> it = entities.iterator();
while (it.hasNext()) {
Entity e = it.next();
e.tick();
}
}
I tried something like this, but it didn't do anything good at all.
public void run(){
entityThread = new Thread();
entityThread.start();
running = true;
Iterator<Entity> it = entities.iterator();
while(running) {
player.tick();
while (it.hasNext()) {
Entity e = it.next();
e.tick();
try {
entityThread.join();
System.out.println("vyksta join");
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException er) {
er.printStackTrace();
}
}
}
}
I thought about doing a threadpool, but there aren't many things in the iterator. 5 entities are there. How can I do this right?