如果我是在这样的条件下等待(注:
current
是一个
AtomicInteger
和
target
是一个
int
):
while (current.get() < target) {
try {
synchronized (current) {
current.wait();
}
}
catch (InterruptedException ie) {}
}
那么同步的进程应该在while内部(如上所述)还是在while外部,像这样?
synchronized (current) {
while (current.get() < target) {
try {
current.wait();
}
catch (InterruptedException ie) {}
}
}
我的问题是,上面两段代码在实际/功能上有什么区别,什么时候应该使用一段代码而不是另一段代码?
编辑:当另一个线程执行以下操作时,循环退出
if (current.incrementAndGet() >= target) {
synchronized (current) {
current.notify();
}
}