书中的List7.15的代码给出的在多线程中,producer-consumer模型下,如何有效的终止服务的实现方式。在示例代码中,利用reservations来表明还有多少的消息需要取出来,并利用isShutdown来表明服务是否已经终止,通过确保isShutdown和reservations的改变是原子的,来获得整个服务的线程安全。
public class LogService {
private final BlockingQueue<String> queue;
private final LoggerThread loggerThread;
private final PrintWriter writer;
@GuardedBy("this")
private boolean isShutdown;
@GuardedBy("this")
private int reservations;
public void start() {
loggerThread.start();
}
public void stop() {
synchronized (this) {
isShutdown = true;
}
loggerThread.interrupt();
}
public void log(String msg) throws InterruptedException {
synchronized (this) {
if (isShutdown)
throw new IllegalStateException(...);
++reservations;
}
queue.put(msg);
}
private class LoggerThread extends Thread {
public void run() {
try {
while (true) {
try {
synchronized (LogService.this) {
if (isShutdown && reservations == 0)
break;
}
String msg = queue.take();
synchronized (LogService.this) {
--reservations;
}
writer.println(msg);
} catch (InterruptedException e) {
/* retry */ }
}
} finally {
writer.close();
}
}
}
}