Java 按顺序在两个线程中打印两个不同的字符串
我正在尝试编写一个程序,其中两个线程同时运行。一个是印刷品杰克,另一个是琼斯。预期产出为: 杰克·琼斯、杰克·琼斯等等。但我在调用notifyAll()时遇到了问题。谁能告诉我出了什么问题 例外情况Java 按顺序在两个线程中打印两个不同的字符串,java,multithreading,Java,Multithreading,我正在尝试编写一个程序,其中两个线程同时运行。一个是印刷品杰克,另一个是琼斯。预期产出为: 杰克·琼斯、杰克·琼斯等等。但我在调用notifyAll()时遇到了问题。谁能告诉我出了什么问题 例外情况 Starting thread Jack Jones Exception in thread "Thread-0" Exception in thread "Thread-1" java.lang.IllegalMonitorStateException at java.lang.Object
Starting thread
Jack Jones Exception in thread "Thread-0" Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at JonesThread.printJones(JonesThread.java:32)
at JonesThread.run(JonesThread.java:14)
java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at JackThread.printJack(JackThread.java:36)
at JackThread.run(JackThread.java:15)
千斤顶螺纹
import java.util.concurrent.atomic.AtomicBoolean;
public class JackThread extends Thread {
AtomicBoolean i;
public JackThread(AtomicBoolean i2) {
this.i = i2;
}
public void run() {
while (true) {
try {
printJack();
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void printJack() throws InterruptedException {
synchronized (i) {
while (i.get()) {
{
wait();
}
}
System.out.print("Jack ");
i.set(true);
notifyAll();
}
}
}
import java.util.concurrent.atomic.AtomicBoolean;
public class JonesThread extends Thread {
AtomicBoolean i;
public JonesThread(AtomicBoolean i2) {
this.i = i2;
}
public void run() {
while (true) {
try {
printJones();
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void printJones() throws InterruptedException {
synchronized (i) {
while (!i.get()) {
wait();
}
System.out.print("Jones ");
i.set(false);
notifyAll();
}
}
}
琼斯线程
import java.util.concurrent.atomic.AtomicBoolean;
public class JackThread extends Thread {
AtomicBoolean i;
public JackThread(AtomicBoolean i2) {
this.i = i2;
}
public void run() {
while (true) {
try {
printJack();
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void printJack() throws InterruptedException {
synchronized (i) {
while (i.get()) {
{
wait();
}
}
System.out.print("Jack ");
i.set(true);
notifyAll();
}
}
}
import java.util.concurrent.atomic.AtomicBoolean;
public class JonesThread extends Thread {
AtomicBoolean i;
public JonesThread(AtomicBoolean i2) {
this.i = i2;
}
public void run() {
while (true) {
try {
printJones();
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void printJones() throws InterruptedException {
synchronized (i) {
while (!i.get()) {
wait();
}
System.out.print("Jones ");
i.set(false);
notifyAll();
}
}
}
main程序
import java.util.concurrent.atomic.AtomicBoolean;
public class ThreadMain {
public static void main(String args[]) {
AtomicBoolean i = new AtomicBoolean(false);
System.out.println("Starting thread");
JackThread t1 = new JackThread( i); // Will give chance to Print Jack first
JonesThread t2 = new JonesThread(i);// Jones will follow Jack
t1.start();
t2.start();
}
}
等待的定义是如果你说
someObject.wait();
线程将等待,直到有人通知某个对象的监视器。另一个线程可以通过调用
someObject.notify(); // or notifyAll
但问题是,线程必须使用相同的对象进行协调。您尚未指定对象,因此您的wait()
相当于
this.wait();
也就是说,JackThread
对象正在等待有人通知自己。但是没有人通知JackThread
对象。当您的JonesThread
调用notifyAll()
时,它与
this.notifyAll();
因此它会通知自己,即JonesThread
对象。因此,基本上,你的两个线程是在相互交谈,而不是相互交谈
看起来您已经将i
设置为两个线程都知道的对象,因此可以将其用于wait
和notify
,即i.wait()
,i.notifyAll()
免责声明:我还没有测试过它