在Java中使用轮询不好吗?
2022-09-01 09:28:10
我有几个作为数据队列工作。每个队列都链接到一个单独的线程,该线程检查其中是否有一些数据。ArrayLists
ArrayList
while (array.size == 0) {
// nothing
}
// do stuff with one element of the array
// remove element from array
// and call the loop again
我在嵌入式系统编程中也做过类似的事情,但是在Java中使用它是否安全?问题在于通过迭代来浪费过程功率,同时循环得非常快。
可以通过每100ms添加和检查一次来解决,但话又说回来 - 响应时间较慢。Thread.sleep(100)
问题是 - 我需要添加睡眠还是不应该担心这一点?
对更安全/更好的系统检查数组中的新数据有什么建议吗?