Reputation: 2135
I'm writing in Java, using Eclipse.
In my project (a simple RPG game), I have a main thread of game. There is a game loop, tick and render stuff, etc. In the Game object, I keep the GameState object.
public class Game implements Runnable{
public State gameState;
public State menuState; // I keep one open by State.setState(gameState)
private void tick(){
keyManager.tick();
getInput();
if(State.getState() != null)
State.getState().tick();
}
}
Inside it, there is a World object.
public class GameState extends State{
private World world;
public void tick() {
world.tick();
}
}
Then, I have an array of every entity on the map.
public class World {
private EntityManager entityManager;
public World(/*some args*/){
for (int i=0; i<3; i++){
addRandomNPC();
// ^ it does some stuff, but in the end, it uses:
// entityManager.addEntity(new NPC(/*random args*/));
}
}
public void tick(){
entityManager.tick();
}
}
public class EntityManager{
private ArrayList<Entity> entities;
public void tick(){
for(int i = 0;i < entities.size();i++){
Entity e = entities.get(i);
e.tick();
}
entities.sort(renderSorter);
}
}
I add a NPC. I want it to move on it's own (random moves at random direction for a random period of time), so I created a thread inside them. More specifically, I have an AutoMover class, which implements Runnable.
public class NPC extends Creature {
private Automover autoMover;
public void tick(){
randomizeInput();
move();
}
private randomizeInput() {
xMove = autoMover.getxMove();
yMove = autoMover.getyMove();
}
}
public class AutoMover implements Runnable {
private Timer timer
public void run() {
timer = new Timer();
Random gen = new Random();
duration = gen.nextFloat()*Utils.maxAutoMoverDuration;
while(running){
timer.update();
if(timer.timer >= duration*1000000000){
// some simple math about xMove, yMove and duration
duration = gen.nextFloat()*Utils.maxAutoMoverDuration;
timer.timer = 0;
}
}
stop(); // does some extra stuff when closing the thread
}
So... I tried to add 1 NPC. Worked. 2 - worked. 10 - damn! It was like my CPU started to shout at me. Is it possible, that all these threads of 10 NPCs use so much CPU? What would be a better way to do it? I would like to make it really random and independent.
Upvotes: 2
Views: 417
Reputation: 140407
100% are typically an indication that you are not "sleeping" your threads. I also did not spot any sign of that in your code.
So, that is: to be expected. The answer is simply: do not keep your Thread in a "hot" loop; doing nothing but checking the loop condition. You see, a modern CPU can do that (checking that loop condition) many times per second. And it finds that so boring that gets a furious, aka hot about that.
A simple Thread.sleep(50), or 100, or xxx should do here!
Upvotes: 3
Reputation: 1258
It has nothing to do with eclipse here is an example of code that will also use alot of CPU though it does not do any thing special
public static void main(String[] args) {
Thread t = new Thread(() -> {
while (true) {
System.out.println("Printing stuff");
/* try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
t.start();
}
In this code there is no pause between each iteration so the CPU almost never realesd .Notice the commented section if you uncomment it suddenly CPU is not consumed with the same rate.
Upvotes: 1