Код: Выделить всё
public boolean checkMail(Set hosts, long timeout, TimeUnit unit)
throws InterruptedException {
ExecutorService exec = Executors.newCachedThreadPool();
final AtomicBoolean hasNewMail = new AtomicBoolean(false);
try {
for (final String host : hosts)
exec.execute(new Runnable() {
public void run() {
if (checkMail(host))
hasNewMail.set(true);
}
});
} finally {
exec.shutdown();
exec.awaitTermination(timeout, unit);
}
return hasNewMail.get();
}
< /code>
Это написано в книге: < /p>
Причина, по которой Atomicboolean используется вместо летучих логических Использование изменчивого типа Подробнее здесь: https://stackoverflow.com/questions/796 ... tice-7-2-4