Java 无限循环性能
Java infinite loop performance
我有一个只有在特定情况出现时才需要工作的线程。否则它只会迭代一个空的无限循环:
public void run() {
while(true) {
if(ball != null) {
// do some Calculations
}
}
}
当循环实际上什么都不做但它必须检查它是否必须在每次迭代时进行计算时,它会影响性能吗?
仅在需要时创建此线程对我来说不是一个选项,因为我实现 Runnable 的 class 是一个一直显示的可视对象。
编辑:那么下面是不是一个好的解决方案?还是使用不同的方法更好(关于性能)?
private final Object standBy = new Object();
public void run() {
while(true) {
synchronized (standBy) {
while(ball != null) // should I use while or if here?
try{ standBy.wait() }
catch (InterruptedException ie) {}
}
if(ball != null) {
// do some Calculations
}
}
public void handleCollision(Ball b) {
// some more code..
ball = b;
synchronized (standBy) {
standBy.notify();
}
}
是的,肯定会影响性能。为了提高性能,您可以考虑在代码中加入一点时间延迟(比如 500 毫秒或 1000 毫秒甚至更长),具体取决于时间对您的重要性。
在您的话题之间分享 BlockingQueue
。
class Producer implements Runnable {
private final BlockingQueue queue;
Producer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { queue.put(produce()); }
} catch (InterruptedException ex) { ... handle ...}
}
Object produce() { ... }
}
class Consumer implements Runnable {
private final BlockingQueue queue;
Consumer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { consume(queue.take()); }
} catch (InterruptedException ex) { ... handle ...}
}
void consume(Object x) { ... }
}
是的。这是 busy waiting 最简单的实现,应尽可能避免。使用 wait/notify 或 java.util.concurrent
机制。也许您应该更具体地说明您想要实现的目标以获得更多有用的回复。
您可能需要考虑让线程休眠,仅当您的 'ball' 变量变为真时才将其唤醒。有多种方法可以做到这一点,从使用非常低级别的 wait
和 notify
语句到使用 java.util.concurrent
类 提供了一种更不容易出错的方法.查看 condition interface. A data structure like a BlockingQueue 的文档也是一个解决方案。
我发现了以下有趣的事情。在任务管理器中,运行 那样的无限循环会消耗我 CPU 的 17%。现在,如果我添加一个简单的
Thread.sleep(1)
在只有一毫秒的循环中,CPU使用率下降到几乎为零,就好像我没有使用该程序一样,程序的平均响应时间仍然相当不错(在我的情况需要快速回复)
我有一个只有在特定情况出现时才需要工作的线程。否则它只会迭代一个空的无限循环:
public void run() {
while(true) {
if(ball != null) {
// do some Calculations
}
}
}
当循环实际上什么都不做但它必须检查它是否必须在每次迭代时进行计算时,它会影响性能吗? 仅在需要时创建此线程对我来说不是一个选项,因为我实现 Runnable 的 class 是一个一直显示的可视对象。
编辑:那么下面是不是一个好的解决方案?还是使用不同的方法更好(关于性能)?
private final Object standBy = new Object();
public void run() {
while(true) {
synchronized (standBy) {
while(ball != null) // should I use while or if here?
try{ standBy.wait() }
catch (InterruptedException ie) {}
}
if(ball != null) {
// do some Calculations
}
}
public void handleCollision(Ball b) {
// some more code..
ball = b;
synchronized (standBy) {
standBy.notify();
}
}
是的,肯定会影响性能。为了提高性能,您可以考虑在代码中加入一点时间延迟(比如 500 毫秒或 1000 毫秒甚至更长),具体取决于时间对您的重要性。
在您的话题之间分享 BlockingQueue
。
class Producer implements Runnable {
private final BlockingQueue queue;
Producer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { queue.put(produce()); }
} catch (InterruptedException ex) { ... handle ...}
}
Object produce() { ... }
}
class Consumer implements Runnable {
private final BlockingQueue queue;
Consumer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { consume(queue.take()); }
} catch (InterruptedException ex) { ... handle ...}
}
void consume(Object x) { ... }
}
是的。这是 busy waiting 最简单的实现,应尽可能避免。使用 wait/notify 或 java.util.concurrent
机制。也许您应该更具体地说明您想要实现的目标以获得更多有用的回复。
您可能需要考虑让线程休眠,仅当您的 'ball' 变量变为真时才将其唤醒。有多种方法可以做到这一点,从使用非常低级别的 wait
和 notify
语句到使用 java.util.concurrent
类 提供了一种更不容易出错的方法.查看 condition interface. A data structure like a BlockingQueue 的文档也是一个解决方案。
我发现了以下有趣的事情。在任务管理器中,运行 那样的无限循环会消耗我 CPU 的 17%。现在,如果我添加一个简单的
Thread.sleep(1)
在只有一毫秒的循环中,CPU使用率下降到几乎为零,就好像我没有使用该程序一样,程序的平均响应时间仍然相当不错(在我的情况需要快速回复)