[英]Weird behavior with Print Statements in Java
This is a weird thing I ran into while trying to make a game in Java.这是我在尝试用 Java 制作游戏时遇到的一件奇怪的事情。 When I run this as an implementation of the run method in the Runnable interface it gives me a blank screen with no output of the console
当我在 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();
However, by adding a print statement that prints out the integer timer, I'm able to display a green screen (which I want) and the FPS is being displayed to the console.但是,通过添加打印整数计时器的打印语句,我可以显示绿屏(这是我想要的)并且 FPS 正在显示到控制台。
Thoughts???想法???
Your question contains nowhere like enough information to understand what is actually happening.您的问题没有足够的信息来了解实际发生的情况。 (Please consider providing a minimal reproducible example ).
(请考虑提供一个最小的可重现示例)。
However, this sounds like a typical problem when multiple threads are involved, and one thread is updating stared state without adequate synchronization.然而,当涉及多个线程时,这听起来像是一个典型的问题,并且一个线程在没有足够同步的情况下更新启动状态。
Thoughts???
想法???
Well ... the standard i/o stream stacks have internal synchronization so that (for example) multiple threads writing to System.out doesn't cause corruption.嗯……标准的 i/o 流堆栈具有内部同步,因此(例如)写入 System.out 的多个线程不会导致损坏。
This synchronization can lead to cache flushes for other memory address, and alter the behavior of an incorrectly implemented multi-threaded application.这种同步会导致其他内存地址的缓存刷新,并改变错误实现的多线程应用程序的行为。 Hence, adding
print
statements can make things work in mysterious ways.因此,添加
print
语句可以使事情以神秘的方式工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.