【发布时间】:2015-04-19 22:07:22
【问题描述】:
所以我正在写一个视频播放库,细节并不重要。这是怎么回事,这段代码需要在解码器线程的run()方法结束时运行:
System.out.println("Video decoding complete");
int a = 0, b = 0;
do
{
a = pictures.getCount();
b = samples.getCount();
}while(a > 0 || b > 0);
Gdx.app.log("Status", videoPath + " completed playing successfully.");
videoComplete = true;
问题是,通过 do{}while 的任何内容都不会执行。{}这是奇怪的部分,这段代码是在将 System.out.println 添加到 while 循环中时执行的:
System.out.println("Video decoding complete");
int a = 0, b = 0;
do
{
System.out.println("Waiting for packets to drain.");
a = pictures.getCount();
b = samples.getCount();
}while(a > 0 || b > 0);
Gdx.app.log("Status", videoPath + " completed playing successfully.");
videoComplete = true;
我怀疑编译器知道我试图让它运行一个循环,它暂时什么都不做,它只是剪掉了代码或其他东西。但我真的不知道发生了什么。如果有人比我更了解,我希望有更好的解决方案。这么简单的事情,我就挂在这里了!
【问题讨论】:
-
不,当编译器改变程序的行为时,它永远不会删除代码位。你怎么知道他们没有被处决?
-
一开始为什么要“让它运行一个不做任何事情的循环”?
-
我不知道您所看到的原因,但您所拥有的是一个“忙等待”,它将占用 CPU 直到计数达到零。阅读线程同步,并使其 wait() 用于其他线程。
-
贴一个完整的例子,我们可以重现,否则很难调试。
-
@slim - 是的,好电话。下面的答案有效,但我最终接受了你的建议。 :D
标签: java multithreading compiler-optimization