簡體   English   中英

Java 中打印語句的奇怪行為

[英]Weird behavior with Print Statements in Java

這是我在嘗試用 Java 制作游戲時遇到的一件奇怪的事情。 當我在 Runnable 界面中將它作為 run 方法的實現運行時,它給了我一個沒有控制台輸出的空白屏幕

public void run() {
    //Grabs the last time from the CPU in nano-seconds
    long lastTime = System.nanoTime();
    //Amount of ticks that we want per second
    double amountOfTicks = 60.0;
    //Time alloted for one tick in nano-seconds for optimal performance
    double ns = 1000000000 / amountOfTicks;
    //Change in time from the CPU to check how overloaded it is
    double delta = 0;
    //Used to calculate FPS
    long timer = System.currentTimeMillis();
    int frames = 0 ;

    /*  Game loop use:
     *      From timer calls to while loops to recursive function calls to keep the game alive 
     *      Until a certain condition is meet (The loop)
     * 
     *      A way to delay the length of each loop iteration so you get a stable frame rate (The timing mechanism) 
     *  
     *      A way to make the game speed remain independent of the speed of the loop (The interpolation)*/

    //This is a variable timestep loop
    while (running) {
        //Grabs current time
        long now = System.nanoTime();
        //The change from now to lasttime divided by the the optiaml time for ticks 
        delta += (now - lastTime) / ns;
        lastTime = now;
        //If the change is greater than 1, that means the mimimum time for a tick has been reached and it can call the tick method
        while (delta >= 1) {
            tick();
            //Resets delta
            delta--;
        }
        //If game is running call the render method
        if(running)
            render();
        frames++;


        //If more than a second has passed in the CPU, print the number of frames and reset all values for the next second
        if(System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            System.out.println("FPS: " + frames);
            frames = 0;
            //While times updated per second is greater than one (allowing updates to be seen) call tick method of Handler class


            /* Fixed timestep loops vs Variable timestep loops:
             * 
             *      Variable timestep loops: Great because the game will seem consistent regardless of how fast the players's computer is
             *          Allowing it to work on a variety of machines and update the game logic with very high attention to details happening in the game 
             *          Graphics latency is not a problem as things are drawn as they change
             * 
             *      
             *    
             *      Fixed timestep loops: You know that every single timestep 
             *          will take up the exact same length of time. Allows for consistent gameplay 
             *          regardless of how fast a machine is as you know the tick rate of your machine
             *          Works wonderfully for math-heavy games with physics operations. Also good for networked games as packets are going and coming at a generally constant speed.
             *          Keeps game logic running at a very low rate while the frame rate can still and frame rate really high 
             *                                                            */
        }
    } 
    stop();

但是,通過添加打印整數計時器的打印語句,我可以顯示綠屏(這是我想要的)並且 FPS 正在顯示到控制台。

想法???

您的問題沒有足夠的信息來了解實際發生的情況。 (請考慮提供一個最小的可重現示例)。

然而,當涉及多個線程時,這聽起來像是一個典型的問題,並且一個線程在沒有足夠同步的情況下更新啟動狀態。

想法???

嗯……標准的 i/o 流堆棧具有內部同步,因此(例如)寫入 System.out 的多個線程不會導致損壞。

這種同步會導致其他內存地址的緩存刷新,並改變錯誤實現的多線程應用程序的行為。 因此,添加print語句可以使事情以神秘的方式工作。

暫無
暫無

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

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