等待更新
等待事件/更新的最佳方式是什么?例如,我正在等待这个数据结构更新后再做某事。在循环内实现它并不是最好的方法,因为它会消耗大量的 CPU 时间,例如:
while (true) {
// keep checking the data structure
// if data structure is updated
// break;
}
// do something here
在 Java 中实现此类内容的简单但有效的方法是什么?
What is the best way to wait for an event/update. For example, I am waiting for this data structure to be updated before doing something. Implementing it inside a loop is not the best way since it consumes much CPU time like:
while (true) {
// keep checking the data structure
// if data structure is updated
// break;
}
// do something here
What's a simple but efficient way to implement something like this in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
wait-notifyAll 是比循环更有效的方法。
wait() 的标准习惯用法:
但它是控制线程的原始方法,您最好使用 java.util.concurrent 包中的类。而且,如果我遇到这样的问题,我会选择Chris Dail的答案。
wait-notifyAll is more efficient way than loop.
Standard idiom for wait():
But it's primitive way to control threads, you'd better use classes in
java.util.concurrent
package. Moreover, I will choose Chris Dail's answer if I meet such problem.这实际上取决于程序其余部分的结构。我可能会首先查看 java.lang. util.concurrent 看看里面的东西是否适合你。
实现此目的的方法示例:
Futures - 如果您有一些“工作”要做,您可以使用线程池执行器服务来执行该工作。当您调用 submit() 时做你的工作,你就会得到一个你可以检查或阻止的未来,直到工作完成。
队列 - 如果您有一个组件执行工作,而一个组件执行等待,则可以通过队列完成它们的通信。每当人们处理完数据后,它就可以添加到队列中。您可以使用 LinkedBlockingQueue 和 poll() 来完成工作。
侦听器 - 根本没有并发,您可以使用侦听器/观察者模式。
根据您的应用程序结构,有许多不同的选项。
It really depends on the structure of the rest of your program. I would probably start by looking through java.util.concurrent to see if something in there suits you.
Examples of ways you could do this:
Futures - If you have some 'work' to be done, you can have a thread pool executor service to perform the work. When you call submit() to do your work, you get a future that you can check or block until the work is completed.
Queues - If you have one component doing the work and one component doing the waiting, you could have their communication done with queues. Any time one is done with working on the data, it can add to a queue. You could use the LinkedBlockingQueue and poll() for the work to be completed.
Listeners - Without concurrent at all, you could use the Listener/Observer pattern.
There are lots of different options depending on your application structure.
这是我要做的代码示例。
在此逻辑中,我在线程中使用
join
方法。这可确保在主线程继续执行之前所有线程都已加入。我已将 TODO 放在您需要添加代码的位置This is a code sample i would do.
In this logic I use
join
method in threads. This makes sure all the threads are joined before the execution of the main thread continues. I have put TODO for locations u need to add your code假设您在应用程序中使用多线程。要将一个对象与多个线程一起使用,您应该使用同步。当一个线程初始化数据结构时,其他线程等待初始化完成。此逻辑通常使用可以在任何对象上调用的等待/通知方法来实现。
工作线程:
初始化线程:
object
是需要初始化的数据结构。initialized
标志用于指示初始化已完成。最好使用此标志,因为有时wait
可能会在没有相应的notify
的情况下完成。Assuming that you use multi-threading in an application. To use one object with several threads you should use synchronization. While one thread initializes data structure, other wait for finishing of initialization. This logic is usually implemented using wait/notify methods which can be called on any object.
Working thread(s):
Initialization thread:
object
is the data structure which should be initialized. Theinitialized
flag used to indicate that the initialization has completed. It is better to use this flag because sometimeswait
can be finished without correspondednotify
.