没有像while这样昂贵的循环,任何人都可以编写一个无限运行的线程吗?
就像我不想要这样的东西
public void run() {
while(true)
{
do Blah Blah Blah
Thread.sleep(.....);
}
}
编写如此长时间运行的线程的任何优化方式。
提前致谢。
没有像while这样昂贵的循环,任何人都可以编写一个无限运行的线程吗?
就像我不想要这样的东西
public void run() {
while(true)
{
do Blah Blah Blah
Thread.sleep(.....);
}
}
编写如此长时间运行的线程的任何优化方式。
提前致谢。
我发现while (true) { ... }
这是实现普通无限循环的最干净的方法。我认为没有更紧凑或更具成本效益的方式来做到这一点。
虽然有些人更喜欢for (;;) { ... }
,但我认为这看起来很尴尬。
在成本方面,javac
两者都编译为
public static void main(java.lang.String[]);
Code:
0: ...
...
...
goto 0
}
如果你确实希望循环是无限的,我看不到任何更有效的方法。
由于您Thread.sleep
的代码中有 a ,我还建议您查看例如java.util.Timer
和java.util.TimerTask
.
If your loop tries to do something repeatedly, you are right that you're probably wasting CPU time in the long run. Especially if you happen to have many instances of this type of thread. In this case I would recommend you to look into the observer pattern. Let the object waiting for something, listen to the objects affecting the possibility of succeeding with what it's trying.
In what way do you consider while(true)
"costly"? Do you have any evidence that this is affecting your performance? What operation would you consider to be cheaper than it?
Note that from your comment:
I guess such a infinite while loop blocks your CPU. Isn't dat? So I consider it costly.
it sounds like you're considering the whole loop, not the while
part. So let's look at what you're doing in the loop:
do Blah Blah Blah
Thread.sleep(.....);
You're explicitly telling the thread to sleep. While it's sleeping, it won't be consuming CPU resources. So no, it's not costly. Admittedly if your sleep period is very short and your "do Blah Blah Blah" is very quickly, you'll be looping an awful lot - but in that case you should consider increasing the sleep time. (You should also consider other approaches which allow you to signal to the thread that you wish it to quit cleanly, and do so even while it's sleeping, without having to interrupt the thread.)
You haven't given us any information about what you're really trying to achieve, but fundamentally the use of while
isn't a problem here. Just think about how often you want to loop, and whether it's really a simple time-based value. For example, if this thread is meant to process work, then perhaps you want a producer/consumer queue of some description - there's no need for the thread to sleep while it has work to do, but there's no need for it to wake up until there's work to do. That's only an example, of course - without more information we really can't give much more advice.
Since we're guessing what you've defined as cost, I thought I'd let you know of a possible cost that I can think of.
A thread usually has some stack memory associated with it. The amount of stack memory associated with a thread for many applications, such as desktop/user applications, isn't really an issue. But say you're developing a server that has to handle a long term connection from each of it's numerous users. And lets say that for every connected user, the server has to perform the same task repetitively (like what you might be doing in your while loop). Addressing this solution with a thread per connection/user can become quite costly. So instead of blocking/sleeping on the thread, it can often be more efficient to ask a thread from a ThreadPool to do the same task whenever you get the event to do so. This can drastically improve memory usage. I think such a optimization could be considered whenever you can have lots of threads relative to the amount of memory available.
Probably not the case for you since you said "indefinite", but it was an interesting problem that I came across recently so I thought I'd blab about it.