İşte kod:Bu koddaki sayacı değiştirmek için Collection.size() öğesini kullanabilir miyim?
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; // <-- counter
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();
}
}
}
}
Bu Uygulama kitabın Java eşzamanlılık bir pasajı, ve ben biz sadece sayısını almak için queue.size()
kullanmaya devam edebileceği için belki sayaç reservations
gereksiz olduğunu düşünüyorum queue
öğesinde.
Doğru muyum?
Sırasıyla "koy" ve "al" senkronize edilmez. 'rezervasyonları' değişikliği. –