簡體   English   中英

Java 線程在 join() 中被阻塞

[英]Java Thread is blocked in join()

我有以下簡單的代碼,我在其中放入和取出表示為 ArrayList 的隊列。

public class EmailService {

    private Queue<Email> emailQueue;
    private Object lock;
    private volatile boolean run;
    private Thread thread;

    public void sendNotificationEmail(Email email) throws InterruptedException {
        emailQueue.add(email);

        synchronized (lock) {
            lock.notify();
            lock.wait();
        }
    }

    public EmailService() {
        lock = new Object();
        emailQueue = new Queue<>();
        run = true;
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (run) {
                    System.out.println("ruuuning");
                    synchronized (lock) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (emailQueue.getSize() > 0) {
                            sendEmail(emailQueue.poll());
                        }
                        lock.notify();
                    }
                }
            }
            private void sendEmail(Email email) {
                System.out.println("Sent email from " + email.getFrom() + " to " + email.getTo() + " with content: " + email.getContent());
            }
        });
        thread.start();
    }

    public void close() throws InterruptedException {
        run = false;
        synchronized (lock) {
            lock.notify();
            System.out.println("Thread will join " + thread.isInterrupted());
            thread.join();
            System.out.println("Thread after join");
        }
    }
}

我不明白為什么我的線程在join()方法中被阻塞。 從主要我調用如下:

eService = new EmailService();
Email e1 = new Email(client1, client2, "content1");
eService.sendNotificationEmail(e1);
eService.close();

不運行它...

  • close()方法在調用thread.join()時持有lock並等待thread (永遠)
  • thread正在等待重新獲取lock因此無法運行

雙方現在都在互相等待,這是一個僵局 嘗試在synchronized塊之后移動Thread.join()

    public void close() throws InterruptedException {
        run = false;
        synchronized (lock) {
            lock.notify();
            System.out.println("Thread will join " + thread.isInterrupted());
        }
        thread.join();
        System.out.println("Thread after join");
    }

@drekbour 解釋了您的程序如何掛在join()調用中,但僅供參考:這是您的程序掛起的一種方式。 這稱為丟失通知。

您的主線程創建一個新的EmailService實例。 新實例創建它的線程並調用thread.start() *但 * 線程實際開始運行可能需要一些時間。 同時...

您的主線程創建一個新的Email實例,並調用eService.sendNotificationEmail(...) 該函數將新消息添加到隊列中,鎖定lock ,通知鎖,然后等待鎖。

最后,服務線程啟動,進入其run()方法,鎖定鎖,然后調用lock.wait()

此時程序會卡住,因為每個線程都在等待對方的通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM