gpt4 book ai didi

java - 使用wait()和notify()进行同步

转载 作者:行者123 更新时间:2023-11-30 02:06:22 25 4
gpt4 key购买 nike

我需要使用 3 个不同的线程打印以下图案:

线程 1 打印“I”

线程 2 打印“LOVE”

线程 3 打印“EARTH”

I LOVE EARTH
I LOVE EARTH
I LOVE EARTH

使用wait()和notify()方法。我从以下代码开始,但似乎只打印一次,因为它们都在每个循环的第一次迭代结束时等待。

public class MultiThreading_2 {
static volatile boolean flag=false;
static volatile String word = "I";


public static void main(String[] args) throws InterruptedException {

MultiThreading_2 m = new MultiThreading_2();


Runnable a = new Runnable() {

public void run() {
if(word.equals("I"))
{
synchronized(m)
{
for(int i=1;i<=2;i++) {

if(word.equals("I")) {
System.out.println("I ");
word="LOVE";

try {
m.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
m.notify();

}

}
}
}


};

Runnable b = new Runnable() {

public void run() {

if(word.equals("LOVE"))
{
synchronized(m)
{

for(int j=1;j<=2;j++) {

if(word.equals("LOVE")) {
System.out.println("LOVE ");
word="WORLD";
try {
m.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.notify();


}

}


}
}
}



};

Runnable c = new Runnable() {

public void run() {

if(word.equals("WORLD"))
{
synchronized(m)
{

for(int k=1;k<=2;k++) {
System.out.println("WORLD ");
word="I";
try {
m.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

m.notify();



}


}

}
}



};


new Thread(a).start();
Thread.sleep(100);
new Thread(b).start();
Thread.sleep(100);
new Thread(c).start();

}


}

有人可以解释一下如何解决这个问题吗?

最佳答案

我已经修复了你的代码。您已将“m.notify()”放在“m.wait()”之后,以便它们都互相等待。我轻轻地将它移到“m.wait()”之前,并将 for 循环转换为无限的 while 循环,以使线程永远运行。

更新1

我已经更新了代码,以便线程将文本写入三次。

public class MultiThreading_2 {
static volatile boolean flag = false;
static volatile String word = "I";

public static void main(String[] args) throws InterruptedException {

MultiThreading_2 m = new MultiThreading_2();

Runnable a = new Runnable() {

public void run() {

for (int i = 0; i < 3; i++) {
synchronized (m) {
if (word.equals("I")) {
System.out.print("I ");
word = "LOVE";

m.notify();
try {
m.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
i--;
}

}

}
}

};

Runnable b = new Runnable() {

public void run() {

for (int i = 0; i < 3; i++) {
synchronized (m) {
if (word.equals("LOVE")) {
System.out.print("LOVE ");
word = "WORLD";
m.notify();
try {
m.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else {
i--;
}

}

}
}

};

Runnable c = new Runnable() {

public void run() {

for (int i = 0; i < 3; i++) {
synchronized (m) {
if (word.equals("WORLD")) {
System.out.println("WORLD ");
word = "I";
m.notify();
try {
m.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else {
i--;
}
}

}

}

};

new Thread(a).start();
Thread.sleep(100);
new Thread(b).start();
Thread.sleep(100);
new Thread(c).start();

}

}

关于java - 使用wait()和notify()进行同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51231146/

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