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