第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用此關(guān)鍵字作為并發(fā)鎖

使用此關(guān)鍵字作為并發(fā)鎖

SMILET 2021-05-31 16:15:48
在下面的程序中,LoggerThread類中的this關(guān)鍵字是指LoggerThread對象還是LogService對象?從邏輯上講,它應(yīng)該引用 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();            }        }    }}感謝您的幫助
查看完整描述

3 回答

?
慕娘9325324

TA貢獻1783條經(jīng)驗 獲得超4個贊

thisLoggerThread方法內(nèi)是指一個LoggerThread實例。
LogService.this指的是外部類。


二者isShutdownreservations通過不同的鎖(同步LoggerThread.thisLogService.this),因此@GuardedBy("this")不反映現(xiàn)實。


查看完整回答
反對 回復 2021-06-02
?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

this指的是直接封閉類的當前實例。JLS #15.8.4 .

從邏輯上講,它應(yīng)該引用 LogService 以便同步工作,但從語義上講,它似乎指的是 LoggerThread。

正確的。這是一個錯誤。


查看完整回答
反對 回復 2021-06-02
  • 3 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號