gpt4 book ai didi

java - 需要了解我的示例中的 wait() 和 notification() 行为吗?

转载 作者:行者123 更新时间:2023-12-01 15:21:29 25 4
gpt4 key购买 nike

public class class_Q {
volatile boolean valueSet = false;
volatile int n;



synchronized int get()
{
System.out.println("Now i am in get block and valueset is : "+ valueSet );
if(!valueSet)
{
System.out.println("i am waiting in get block.....and releasing lock ");
try{
wait();
}catch(InterruptedException e)
{
System.out.println( "InterruptedException caught" );
}
}
System.out.println( " value of n now in get block is : " + n );
valueSet=false;
notify();
return n;
}


synchronized void put(int n)
{
System.out.println(" Now i am in Put block and valueset is : "+ valueSet);
if(valueSet)
{
try
{
System.out.println("i am waiting in put block......and releasing lock. ");
wait();
}catch(InterruptedException e)
{
System.out.println( "InterruptedException caught" );
}
}
this.n = n;
valueSet = true;
System.out.println( "the value of n now in put block is : " + n );
notify();
}

}

 class Producer implements Runnable{
class_Q q;
Producer(class_Q q)
{
this.q = q;
new Thread( this, "Producer" ).start();
}
public void run()
{
int i = 0;
while(true)
{
q.put(i++);
}
}
}


class Consumer implements Runnable{
class_Q q;
Consumer(class_Q q)
{
this.q = q;
new Thread(this, "Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}


class PCFixed {
public static void main (String args[])
{
class_Q q = new class_Q();
new Producer(q);
new Consumer(q);
System.out.println( "Press Control-C to stop." );
}
}

*OUTPUT**

现在我处于 get block 中,值集是: false

我正在等待 get block ......并释放锁

按 Control-C 停止。

现在我在 Put block 中,值集是: false

现在 put block 中 n 的值为:0

现在 get block 中 n 的值为:0

现在我处于 get block 中,值集是: false

我正在等待 get block ......并释放锁

现在我在 Put block 中,值集是: false

现在 put block 中 n 的值为:1

现在 get block 中 n 的值为:1

在输出的第六行之后,我期望 get() 线程唤醒(“notify()”)put() 线程。有人可以帮助我理解调用 get() 线程背后的逻辑(换句话说,为什么它在 get block 中?)

最佳答案

我已经重新格式化了您的代码并更改了日志消息,因此一切都应该更加清晰。

public class Test {
static class class_Q {
volatile boolean valueSet = false;
volatile int n;

synchronized int get() throws InterruptedException {
System.out.println("get entering - valueSet=" + valueSet);
// *** Changed from `if` to `while`
while (!valueSet) {
System.out.println("get waiting");
wait();
}
// Clear to set the value.
valueSet = false;
// Tell any put waits to finish
notify();
System.out.println("get finished - n=" + n);
return n;
}

synchronized void put(int n) throws InterruptedException {
System.out.println("put entering - valueSet=" + valueSet);
// *** Changed from `if` to `while`
while (valueSet) {
System.out.println("put waiting");
wait();
}
this.n = n;
valueSet = true;
System.out.println("put finished - n=" + n);
notify();
}
}

static class Producer implements Runnable {
class_Q q;

Producer(class_Q q) {
this.q = q;
}

public void run() {
int i = 0;
try {
while (true) {
q.put(i++);
System.out.println("put(" + (i-1) + ")");
}
} catch (InterruptedException ex) {
// Just exit the run loop and finish when interrupted.
}
}
}

static class Consumer implements Runnable {
class_Q q;

Consumer(class_Q q) {
this.q = q;
}

public void run() {
try {
while (true) {
int i;
i = q.get();
System.out.println("get(" + i + ")");
}
} catch (InterruptedException ex) {
// Just exit the run loop and finish when interrupted.
}
}
}

public static void main(String args[]) {
class_Q q = new class_Q();
Thread producer = new Thread(new Producer(q));
Thread consumer = new Thread(new Consumer(q));
System.out.println("Press Control-C to stop.");
producer.start();
consumer.start();
}
}

我也做了三项主要改变。我已经使中断机制只是退出你的线程。我已经让您的阻塞测试在阻塞状态下循环,而不仅仅是检查它们(while (x) 而不是 if (x))。我已经让你的线程不自动启动。

我认为如果您现在运行这段代码,流程应该会更清晰,您应该能够更好地理解正在发生的事情。请记住,System.out 是一个PrintWriter,因此可以进行缓冲。

我得到的输出是:

put entering - valueSet=falseput finished - n=0put(0)put entering - valueSet=trueput waitingPress Control-C to stop.get entering - valueSet=trueget finished - n=0put finished - n=1put(1)put entering - valueSet=trueput waitingget(0)get entering - valueSet=trueget finished - n=1put finished - n=2put(2)put entering - valueSet=trueput waitingget(1)get entering - valueSet=trueget finished - n=2get(2)get entering - valueSet=falseget waitingput finished - n=3get finished - n=3get(3)get entering - valueSet=falseget waitingput(3)put entering - valueSet=falseput finished - n=4get finished - n=4get(4)get entering - valueSet=falseget waitingput(4)...

关于java - 需要了解我的示例中的 wait() 和 notification() 行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10849758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com