簡體   English   中英

System.out.println在while循環的每個迭代中打印兩次

[英]System.out.println printing twice in each iteration of while loop

我試圖找出為什么我收到印有while循環的每次迭代兩行,當我運行這段代碼:

public void run() {
    long currentTime = System.nanoTime();
    long targetFPS = 1000;
    long delta;
    while(GameConstants.running) {
        delta = System.nanoTime() - currentTime;
        if(delta/1000000000 >= 1) {
            System.out.println(delta);
            currentTime = System.nanoTime();
        }
    }
}

我嘗試將代碼調整為僅調用System.out.println()一次即可進行調試,但是它仍然打印兩次。 我似乎無法弄清楚為什么會發生這種情況,因為在此修改版本中,將調用System.out.println(delta) ,然后將System.out.println(delta)設置為true,因此不應再次調用System.out.println(delta)

public void run() {
    long currentTime = System.nanoTime();
    long targetFPS = 1000;
    long delta;
            boolean printed = false;
    while(GameConstants.running) {
        delta = System.nanoTime() - currentTime;
        if(delta/1000000000 >= 1 & !printed) {
            System.out.println(delta);
            currentTime = System.nanoTime();
                            printed = true;
        }
    }
}

上下文代碼:

public class Game extends Canvas implements Runnable, KeyListener {

    private Thread gameThread;

public synchronized void start() {
    if(GameConstants.running) return;
    GameConstants.running = true;
    gameThread = new Thread(this);
    gameThread.start();
}

public synchronized void stop() {
    if(!GameConstants.running) return;
    GameConstants.running = false;
    try {
        gameThread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    long currentTime = System.nanoTime();
    long targetFPS = 1000;
    long delta;
            boolean printed = false;
    while(GameConstants.running) {
        delta = System.nanoTime() - currentTime;
        if(delta/1000000000 >= 1 && !printed) {
            System.out.println(delta);
            currentTime = System.nanoTime();
                            printed = true;
        }
    }
}

public void update() {
    if(GameConstants.gameState == 1 && !GameConstants.trialTypeDetermined ) {
        Random r = new Random();
        double prob = r.nextDouble();
        if(prob < GameConstants.goProb) {
            GameConstants.skipBad = true;
        }
        GameConstants.trialTypeDetermined = true;
        System.out.println(GameConstants.skipBad);
    }
      taskTimer(GameConstants.goTaskTimes.get(getGameState(GameConstants.gameState)));
    System.out.println(GameConstants.gameState);
    try {
        Thread.sleep(1);
    } catch(InterruptedException e) {
    }
}

public void render() {

}

//
public String getGameState(int gameState){
    return GameConstants.gameStates[GameConstants.gameState];
}

private void skipBad() {
    Random r = new Random();
    double prob = r.nextDouble();
    if(prob < GameConstants.goProb) {
        GameConstants.skipBad = true;
    }
    GameConstants.trialTypeDetermined = true;
}

public void taskTimer(long taskTime) {
    if(System.currentTimeMillis() - GameConstants.startTime >= taskTime) {
        GameConstants.taskComplete = true;
        GameConstants.startTime = System.currentTimeMillis();
        nextGameState();
        GameConstants.keyPressed = false;
    }
}

private void nextGameState() {
    if(GameConstants.gameState == 2 & GameConstants.skipBad) {
        GameConstants.gameState = GameConstants.gameState + 2;
    } else if(GameConstants.gameState != 5) {
        GameConstants.gameState++;
    } else {
        GameConstants.gameState = 1;
    }
    GameConstants.gameStateRegistered = false;
}

private void resetParameters() {

}

private void checkHit() {

}

public void keyPressed(KeyEvent e){
    int keyCode = e.getKeyCode();

    if(keyCode == KeyEvent.VK_SPACE && !GameConstants.keyPressed){
       GameConstants.keyPressed = true;
       if(!GameConstants.reached){
           GameConstants.reached = true;
           GameConstants.RT = System.currentTimeMillis();
       }
    }
}

public void keyReleased(KeyEvent e){

}

public void keyTyped(KeyEvent e){

}

public static void main(String[] args) {
    Game game = new Game();
    game.setPreferredSize(new Dimension(GameConstants.WIDTH * GameConstants.SCALE, GameConstants.HEIGHT * GameConstants.SCALE));
    game.setMaximumSize(new Dimension(GameConstants.WIDTH * GameConstants.SCALE, GameConstants.HEIGHT * GameConstants.SCALE));
    game.setMinimumSize(new Dimension(GameConstants.WIDTH * GameConstants.SCALE, GameConstants.HEIGHT * GameConstants.SCALE));

    JFrame frame = new JFrame("Game");
    frame.setSize(GameConstants.WIDTH * GameConstants.SCALE, GameConstants.HEIGHT * GameConstants.SCALE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.add(game);
    frame.setVisible(true);
    game.start();
    game.run();
}
}

您的引導程序代碼存在缺陷,您需要同時調用startrun兩次來有效運行該代碼。 刪除要run的電話。

您已在同一個對象上啟動了多個線程,應使用同步塊來運行線程安全代碼,

synchronized(this){

long currentTime = System.nanoTime();
long targetFPS = 1000;
long delta;
while(GameConstants.running) {
    delta = System.nanoTime() - currentTime;
    if(delta/1000000000 >= 1) {
        System.out.println(delta);
        currentTime = System.nanoTime();
    }
}
}

更新源代碼之后

是的 在這里,您正在調用run()方法並同時啟動兩者...

game.start();
game.run();

顯式刪除調用run方法,您將獲得所需的結果:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM