当一个具有相同锁的线程正在睡眠时,看起来另一个线程似乎能够获取锁定?

时间:2017-11-15 21:48:12

标签: java multithreading synchronized

代码下方 - 没有wait()和notify()方法:

package com.jay;

import java.util.Random;

public class Main {

    public static void main(String[] args) {
        Message message = new Message();
        (new Thread(new Writer(message))).start();
        (new Thread(new Reader(message))).start();
    }

}

class Message {
    private String message;
    private boolean empty = true;

    public synchronized String read() {
        while (empty) {

        }
        empty = true;
        return message;
    }

    public synchronized void write(String message) {
        while (!empty) {

        }
        empty = false;
        this.message = message;
    }
}

class Writer implements Runnable {
    private Message message;

    public Writer(Message message) {
        this.message = message;
    }

    public void run() {
        String[] messages = {
            "Humpty Dumpty sat on a wall",
            "Humpty Dumpty had a great fall",
            "All the king's horses and all the king's men",
            "Couldn't put Humpty together again"
        };

        Random random = new Random();

        for(int i=0; i<messages.length; i++) {
            message.write(messages[i]);
            try {
                Thread.sleep(random.nextInt(2000));
            } catch (InterruptedException e) {
                // TODO: handle exception
            }
        }
        message.write("Finished");
    }
}

class Reader implements Runnable {
    private Message message;

    public Reader(Message message) {
        this.message = message;
    }

    public void run() {
        Random random = new Random();

        for(String latestMessage = message.read(); !latestMessage.equals("Finished"); 
                latestMessage = message.read()) {
            System.out.println(latestMessage);

            try {
                Thread.sleep(random.nextInt(2000));
            } catch (InterruptedException e) {
                // TODO: handle exception
            }
        }
    }
}

它在控制台下方打印 - 大部分时间都挂起:

Humpty Dumpty sat on a wall

Humpty Dumpty had a great fall

有时会打印所有四条消息。

我的问题是:当Writer线程已经获得锁并且仍在循环时,Reader线程如何能够在read()方法中执行代码?

我很困惑。请帮我理解!

1 个答案:

答案 0 :(得分:0)

@Oliver查尔斯沃思的评论清除了我的疑虑。我没注意到每次作者离开同步方法时读者都可以输入同步方法。该程序挂起,因为将存在一个线程将在休眠的阶段和&#34;空的&#34;字段不会被修改,导致其中一个循环无限运行。一旦wait()和notify()方法得到实现,问题就会得到解决!