Java wait()是否等待被引用对象或引用本身(如果对引用调用)?
在这种情况下,线程1是否会收到通知(在等待引用而不是对象本身时)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
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();