Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java writer和reader类中的while循环在这里是如何工作的?线程如何更改'empty'变量?_Java_Multithreading_While Loop - Fatal编程技术网

Java writer和reader类中的while循环在这里是如何工作的?线程如何更改'empty'变量?

Java writer和reader类中的while循环在这里是如何工作的?线程如何更改'empty'变量?,java,multithreading,while-loop,Java,Multithreading,While Loop,我在学习java中的多线程时遇到了这个例子-- package com.practice.multi-threading; 导入java.util.Random; 公共类死锁{ 公共静态void main(字符串[]args){ 消息消息=新消息(); (新线程(新写入程序(消息))).start(); (新线程(新读卡器(消息))).start(); } } 类消息{ 私有字符串消息; 私有布尔值为空=真; 公共同步字符串读取(){ while(空){ 试一试{ 等待(); }捕捉(中断异常e

我在学习java中的多线程时遇到了这个例子--

package com.practice.multi-threading;
导入java.util.Random;
公共类死锁{
公共静态void main(字符串[]args){
消息消息=新消息();
(新线程(新写入程序(消息))).start();
(新线程(新读卡器(消息))).start();
}
}
类消息{
私有字符串消息;
私有布尔值为空=真;
公共同步字符串读取(){
while(空){
试一试{
等待();
}捕捉(中断异常e){
}
}
空=真;
notifyAll();
返回消息;
}
公共同步无效写入(字符串消息){
而(!空){
试一试{
等待();
}捕捉(中断异常e){
}
}
空=假;
this.message=消息;
notifyAll();
}
}
类编写器实现可运行的{
私人信息;
公共编写器(消息){
this.message=消息;
}
公开募捐{
字符串消息[]={
“Humpty Dumpty坐在墙上”,
“Humpty Dumpty摔了一跤”,
“所有国王的马和所有国王的人”,
“不能再把汉普提放在一起了”
};
随机=新随机();
对于(int i=0;i false)
  • 当循环设置为空时跳过
  • 然后
    this.message=message
  • 循环的下一次迭代
    ,而(!false)=>true
  • 否控件位于
    while循环中

    现在我在这个循环中找不到任何地方,或者在线程调用方法
    empty
    中找不到变量
    set
    to
    true
    ,这样控制就从
    while循环中产生了。
    那么
    线程如何更改
    空变量并写入和读取消息呢

  • 有人能给我解释一下这个代码的一些迭代吗

    这对我会有很大的帮助。
    提前感谢!

    您可以将消息类视为单个消息缓冲区。当您向其写入消息时,它将变满,并且在读取消息之前无法接受新消息。同样,如果您在消息为空时读取消息,它将等待有人向其写入消息

    消息实例开始为空,因此当您向其写入时,它跳过while循环,设置消息,并通知所有等待的读者

    读取之前的另一次写入现在在while循环中等待,因为
    empty
    为false

    如果从线程读取消息并且消息为非空,则read跳过while循环,将
    empty
    设置为true,并通知所有等待的写入者

    当编写器醒来时,它看到
    empty
    为true,写入消息,并再次将
    empty
    设置为false

    package com.practice.multithreading;
    
    import java.util.Random;
    
    public class DeadlockMain {
        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) {
                try {
                    wait();
                } catch(InterruptedException e) {
                }
            }
            empty = true;
            notifyAll();
            return message;
        }
    
        public synchronized void write(String message) {
            while(!empty) {
                try {
                    wait();
                } catch(InterruptedException e) {
                }
            }
            empty = false;
            this.message = message;
            notifyAll();
        }
    }
    
    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) {
    
                }
            }
            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) {
    
                }
            }
        }
    }