new三个线程,控制输出 依次调用 顺序输出 A B C

线程练手 三个线程依次调用 顺序输出 A B C

写完代码一直在报 IllegalMonitorStateException,current thread is not owner ,我已经加了obj.notify(); 后来我又换成了obj.notifyALl();也还是在报错,后来发现 我放在synchronized锁外了,

synchronized (obj) {
			System.out.println("BB");
			try {
				obj.wait();
				obj.notify();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

报错解决之后又有了新的问题, 开了三个线程,循环了100次调用,但是只输出了A就停止了 ,DeBug了一下,按下一个方法直接跳出去了? 跳没了? 我就莫名其妙,后来我发现了
下面是错误的代码
原因我目前只能解释为: run是线程的一个方法 start才是开始线程

public static void main(String[] args) {

	Object obj = new Object();

	T1 t1 = new T1(obj);
	T2 t2 = new T2(obj);
	T3 t3 = new T3(obj);

	for (int i = 0; i < 100; i++) {
		t1.run()
		t2.run()
		t3.run()
	}

}

直接贴完整代码吧!

class Num {

static int num = 0;

public void count(int num) {
	this.num = num;
}

}

class T1 implements Runnable {

private Object obj;

public T1(Object obj) {
	this.obj = obj;
}

@Override
public void run() {
	if (Num.num == 0) {
		Num.num = 1;
		synchronized (obj) {
			System.out.println("A");
			try {
				obj.wait();
				obj.notifyAll();
				
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
		}

	}


}

}

class T2 implements Runnable {

private Object obj;

public T2(Object obj) {
	this.obj = obj;
}

@Override
public void run() {
	if (Num.num == 1) {
		Num.num = 2;
		synchronized (obj) {
			System.out.println("B");
			try {

				obj.wait();
				obj.notify();
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
		}
	}


}

}

class T3 implements Runnable {

private Object obj;

public T3(Object obj) {
	this.obj = obj;
}

@Override
public void run() {
	if (Num.num == 2) {
		Num.num = 0;
		synchronized (obj) {
			System.out.println("C");
			try {
				obj.wait();
				obj.notify();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

}

public class demo01 {

public static void main(String[] args) {

	Object obj = new Object();

	T1 t1 = new T1(obj);
	T2 t2 = new T2(obj);
	T3 t3 = new T3(obj);

	for (int i = 0; i < 100; i++) {

			new Thread(t1).start();
            new Thread(t2).start();
            new Thread(t3).start();

	}

}

}
更新一版

if会存在虚假唤醒 换成while循环就可以解决

下面是一个实现该功能的Java代码: ```java public class PrintThreadDemo { public static void main(String[] args) { PrintThread threadA = new PrintThread("E", 5); PrintThread threadB = new PrintThread("D", 5); PrintThread threadC = new PrintThread("U", 5); threadA.setNextThread(threadB); threadB.setNextThread(threadC); threadC.setNextThread(threadA); threadA.start(); threadB.start(); threadC.start(); } static class PrintThread extends Thread { private String content; private int count; private PrintThread nextThread; public PrintThread(String content, int count) { this.content = content; this.count = count; } public void setNextThread(PrintThread nextThread) { this.nextThread = nextThread; } @Override public void run() { for (int i = 0; i < count; i++) { synchronized (this) { System.out.print(content); this.notifyAll(); } synchronized (nextThread) { try { nextThread.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } ``` 该代码中,我们定义了一个 `PrintThread` 类,它继承了 `Thread` 类,代表一个打印线程。每个打印线程包含一个待打印的字符串和打印数。并且,我们为每个打印线程设置了一个 `nextThread` 属性,表示该线程执行完后需要通知的下一个线程线程的 `run()` 方法中,我们使用 `synchronized` 关键字来保证线程安全。每打印完字符串后,该线程调用 `notifyAll()` 方法通知其他线程可以运行了。然后,该线程会等待下一个线程的通知,此时我们使用 `wait()` 方法来释放该线程的锁,让其他线程可以执行。当下一个线程得到锁并执行完后,它会再调用 `notifyAll()` 方法通知其他线程可以执行了。 最后,我们在主线程创建三个 `PrintThread` 对象,并将它们按照顺序连接起来,即线程A的下一个是线程B,线程B的下一个是线程C,线程C的下一个是线程A。然后,我们依次启动这三个线程,它们会交替打印出EDU这三个字符串,每个字符串打印5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值