Java 使用此关键字作为锁定并发

Java 使用此关键字作为锁定并发,java,concurrency,this,inner-classes,Java,Concurrency,This,Inner Classes,在以下程序中,LoggerThread类中的this关键字是否指LoggerThread对象或LogService对象?从逻辑上讲,它应该引用LogService,以便同步工作,但从语义上看,它似乎引用的是LoggerThread public class LogService { private final BlockingQueue<String> queue; private final LoggerThread loggerThread; privat

在以下程序中,LoggerThread类中的this关键字是否指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();
            }
        }
    }
}
公共类日志服务{
私有最终阻塞队列;
专用最终LoggerThread LoggerThread;
私人最终印刷作家;
@GuardedBy(“此”)私有布尔值设置;
@由(“本”)私人int保留担保;
public void start(){loggerThread.start();}
公共停车场(){
已同步(此){IsShutton=true;}
loggerThread.interrupt();
}
公共无效日志(字符串msg)引发InterruptedException{
已同步(此){
如果(isShutdown)
抛出新的非法状态异常(“…”);
++保留;
}
queue.put(msg);
}
私有类LoggerThread扩展线程{
公开募捐{
试一试{
while(true){
试一试{
已同步(此){
如果(isShutdown&&reservations==0)
打破
}
字符串msg=queue.take();
已同步(此){--reservations;}
writer.println(msg);
}捕获(中断异常e){/*重试*/}
}
}最后{
writer.close();
}
}
}
}

感谢您的帮助
中的
LoggerThread
方法引用了
LoggerThread
实例。
LogService。此
引用外部类



isShutdown
Reservation
都是由不同的锁(
LoggerThread.this
LogService.this
)同步的,因此
@GuardedBy(“this”)
不能反映现实。

this
指的是立即封闭类的当前实例

从逻辑上讲,它应该引用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();
            }
        }
    }
}

对。这是一个bug。

这段代码来自伟大的著作《实践中的Java并发》,清单7.15
这是一个打字错误,在“勘误表”部分中提到:

我也这么认为,那么从逻辑上说,我认为这个程序是不正确的,它使用两个不同的锁来同步对同一布尔对象的访问。isShutDown。@DerekXie,顺便说一句,注释仅用于文档目的吗?@DerekXie,你是对的,
isShutdown
保留
都是通过不同的锁(
LoggerThread。此
LogService。此
)同步的。是注释仅用于文档目的。作者使用了非标准的注释。非常感谢。不,这不是虫子。