黑马程序员技术交流社区
标题:
多个线程之间通信的问题。
[打印本页]
作者:
lxww
时间:
2013-3-19 21:48
标题:
多个线程之间通信的问题。
本帖最后由 kingdom_202 于 2013-3-19 22:49 编辑
class Test1 {
public static void main(String[] args) {
final Show s = new Show();
new Thread(){
public void run() {
for(int i=1;i<=3;i++){
try {
s.show1();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}.start();
new Thread(){
public void run() {
for(int i=1;i<=3;i++){
try {
s.show1();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}.start();
new Thread(){
public void run() {
for(int i=1;i<=3;i++){
try {
s.show1();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Show {
private int a = 1;
public synchronized void show1()throws Exception {
if (a!= 1){
wait();
}
System.out.print("我");
System.out.print("爱");
System.out.print("你");
System.out.print("\r\n");
a = 2;
notify();
}
public synchronized void show2() throws Exception {
if(a != 2){
wait();
}
System.out.print("I");
System.out.print("LOVE");
System.out.print("YOU");
System.out.print("\r\n");
a = 3;
notify();
}
public synchronized void show3() throws Exception {
if (a != 3){
wait();
}
System.out.print("I");
System.out.print("LIKE");
System.out.print("YOU");
System.out.print("\r\n");
a = 1;
notify();
}
}
以上代码。如果我开两个进程的时候。可以正常打印。为什么开三个进程就不行了、运行不下去。只能打印第一个。急!急!急!
作者:
杨博
时间:
2013-3-19 21:52
class Demo1_Notify {
public static void main(String[] args) {
final Printer p = new Printer();
new Thread(){
public void run() {
while(true)
try {
p.print1();
} catch(Exception e) {
e.printStackTrace();
}
}
}.start();
new Thread(){
public void run() {
for(;;)
try {
p.print2();
} catch(Exception e) {
e.printStackTrace();
}
}
}.start();
new Thread(){
public void run() {
for(;;)
try {
p.print3();
} catch(Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
class Printer {
private int flag = 1; // 创建一个标记变量, 代表目前应该执行哪个方法
public synchronized void print1() throws Exception {
while (flag != 1) // 执行之前判断, 如果刚刚执行过, 就等待
wait();
System.out.print("传");
System.out.print("智");
System.out.print("播");
System.out.print("客");
System.out.print("\r\n");
flag = 2; // 1执行后轮到2了
notifyAll(); // 执行之后通知另外一个线程
}
public synchronized void print2() throws Exception {
while (flag != 2) // 执行之前判断, 如果刚刚执行过, 就等待
wait();
System.out.print("黑");
System.out.print("马");
System.out.print("程");
System.out.print("序");
System.out.print("员");
System.out.print("\r\n");
flag = 3; // 2结束后又应该回到1
notifyAll(); // 执行之后通知另外一个线程
}
public synchronized void print3() throws Exception {
while (flag != 3)
wait();
System.out.print("i");
System.out.print("t");
System.out.print("c");
System.out.print("a");
System.out.print("s");
System.out.print("t");
System.out.print("\r\n");
flag = 1;
notifyAll();
}
}
复制代码
答案见代码说明
作者:
刘凯
时间:
2013-3-19 22:22
本帖最后由 刘凯 于 2013-3-19 22:25 编辑
楼主没有发现,你的三个线程一直都是在调用的show1()方法么, 跟本就没有调用show2() show3()
当然在第一个线程执行完毕后 a=2
第一个线程循环过来也wait了 notify()了下 由于还没有释放同步锁
所以后边两个线程很可能还没有运行show方法 所以都还没有wait 也就是说 notify()没有唤醒任何线程
这时 第一个线程for循环过来也wait了
其他俩线程得到了执行权,线程if(a!=1) 成立 也都wait了
三个线程都wait了 呀 死锁了呀亲
把后边的两个run方法里的show改了 就好了
作者:
lxww
时间:
2013-3-19 22:34
杨博 发表于 2013-3-19 21:52
答案见代码说明
写的太好了。我下去运行了下。很好。非常感谢!呵呵
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2