使用此关键字作为并发锁
Using this keyword as lock in concurrency
在下面的程序中,LoggerThread中的this关键字class是指LoggerThread对象还是LogService对象?从逻辑上讲,它应该引用 LogService 才能使同步工作,但从语义上讲,它似乎是指 LoggerThread。
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 (this) {
if (isShutdown && reservations == 0)
break;
}
String msg = queue.take();
synchronized (this) { --reservations; }
writer.println(msg);
} catch (InterruptedException e) { /* retry */ }
}
} finally {
writer.close();
}
}
}
}
感谢您的帮助
this
在 LoggerThread
方法中引用了一个 LoggerThread
实例。
LogService.this
指的是外class.
isShutdown
和reservations
都被不同的锁同步(LoggerThread.this
和LogService.this
),所以@GuardedBy("this")
不反映实际情况。
this
指的是直接封闭 class 的当前实例。 JLS #15.8.4.
Logically it should refer to LogService in order for the syncronization to work, but semantically it seems it is referring to LoggerThread.
正确。这是一个错误。
这段代码来自伟大的书“Java 并发实践”,清单 7.15
这是一个打字错误,在“勘误表”部分中提到:
http://jcip.net.s3-website-us-east-1.amazonaws.com/errata.html
在下面的程序中,LoggerThread中的this关键字class是指LoggerThread对象还是LogService对象?从逻辑上讲,它应该引用 LogService 才能使同步工作,但从语义上讲,它似乎是指 LoggerThread。
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 (this) {
if (isShutdown && reservations == 0)
break;
}
String msg = queue.take();
synchronized (this) { --reservations; }
writer.println(msg);
} catch (InterruptedException e) { /* retry */ }
}
} finally {
writer.close();
}
}
}
}
感谢您的帮助
this
在 LoggerThread
方法中引用了一个 LoggerThread
实例。
LogService.this
指的是外class.
isShutdown
和reservations
都被不同的锁同步(LoggerThread.this
和LogService.this
),所以@GuardedBy("this")
不反映实际情况。
this
指的是直接封闭 class 的当前实例。 JLS #15.8.4.
Logically it should refer to LogService in order for the syncronization to work, but semantically it seems it is referring to LoggerThread.
正确。这是一个错误。
这段代码来自伟大的书“Java 并发实践”,清单 7.15
这是一个打字错误,在“勘误表”部分中提到:
http://jcip.net.s3-website-us-east-1.amazonaws.com/errata.html