Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 wait()是否等待被引用对象或引用本身(如果对引用调用)?_Java_Multithreading_Wait_Notify - Fatal编程技术网

Java wait()是否等待被引用对象或引用本身(如果对引用调用)?

Java wait()是否等待被引用对象或引用本身(如果对引用调用)?,java,multithreading,wait,notify,Java,Multithreading,Wait,Notify,在这种情况下,线程1是否会收到通知(在等待引用而不是对象本身时) synchronized方法对给定给synchronized块的表达式求值,以确定使用哪个锁。然后需要对同一对象调用wait、notify等,但是否使用相同的变量无关紧要。代码计算表达式以获取对象 现在,thread1是否等待、通知和唤醒取决于竞争条件的结果。如果thread1在thread2调用notify之前进入wait方法,则thread2将等待,然后在thread2发出通知后唤醒。如果thread2在thread1进入wa

在这种情况下,线程1是否会收到通知(在等待引用而不是对象本身时)


synchronized方法对给定给synchronized块的表达式求值,以确定使用哪个锁。然后需要对同一对象调用wait、notify等,但是否使用相同的变量无关紧要。代码计算表达式以获取对象


现在,thread1是否等待、通知和唤醒取决于竞争条件的结果。如果thread1在thread2调用notify之前进入wait方法,则thread2将等待,然后在thread2发出通知后唤醒。如果thread2在thread1进入wait方法之前调用notify,那么thread1将永远等待

等待引用或该引用指向的对象是一样的,因为在对象上获得了锁。无论有多少个引用,如果它们指向内存中的同一对象wait()、notify()、notifyall()都将无缝工作。查看下面的代码

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class ProducerConsumer {

private Queue<Integer>      queue   = new ArrayBlockingQueue<>(10);
private LinkedList<Integer> list    = new LinkedList<>();
int                         limit   = 10;

public static void main(String[] args) {

    final ProducerConsumer pc = new ProducerConsumer();
    final ProducerConsumer pcRef = pc;

    Thread producer = new Thread(new Runnable() {
        int i = 1;

        @Override
        public void run() {
            while (true) {
                synchronized (pcRef) {
                    while (pc.limit == pc.list.size()) {
                        try {
                            pcRef.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    pc.list.add(i);
                    System.out.println("Producer @work : " + pc.list.size());
                    pcRef.notify();
                }

            }
        }
    });

    Thread consumer = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                synchronized (pc) {
                    while (0 == pc.list.size()) {
                        try {
                            pc.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    int val = pc.list.removeFirst();
                    System.out.println("Consumer @work : " + pc.list.size() + " : " + val);
                    pc.notify();
                }

            }
        }
    });

    producer.start();
    consumer.start();

    try {
        producer.join();
        consumer.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

}

wait()
等待引用的对象。事实上,它没有其他选项,因为在Java中引用是通过值传递的。在您的示例中,
reference
lock
指向memoryTry中的同一个对象,将
lock
设置为null,您就可以得到答案。没有等待引用这样的事情。
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class ProducerConsumer {

private Queue<Integer>      queue   = new ArrayBlockingQueue<>(10);
private LinkedList<Integer> list    = new LinkedList<>();
int                         limit   = 10;

public static void main(String[] args) {

    final ProducerConsumer pc = new ProducerConsumer();
    final ProducerConsumer pcRef = pc;

    Thread producer = new Thread(new Runnable() {
        int i = 1;

        @Override
        public void run() {
            while (true) {
                synchronized (pcRef) {
                    while (pc.limit == pc.list.size()) {
                        try {
                            pcRef.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    pc.list.add(i);
                    System.out.println("Producer @work : " + pc.list.size());
                    pcRef.notify();
                }

            }
        }
    });

    Thread consumer = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                synchronized (pc) {
                    while (0 == pc.list.size()) {
                        try {
                            pc.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    int val = pc.list.removeFirst();
                    System.out.println("Consumer @work : " + pc.list.size() + " : " + val);
                    pc.notify();
                }

            }
        }
    });

    producer.start();
    consumer.start();

    try {
        producer.join();
        consumer.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

}
pc.list.add(i);
System.out.println("Producer @work : " + pc.list.size());
//pcRef.notify();