黑马程序员技术交流社区

标题: 线程间通信有关问题 [打印本页]

作者: zhoubinjian    时间: 2016-4-5 02:16
标题: 线程间通信有关问题
本帖最后由 zhoubinjian 于 2016-4-7 17:26 编辑

/*
以下代码已更正,谢谢黑友们的回复。。
*/

import java.util.concurrent.locks.*;
class Res
{
        private String name;
        private String sex;
        boolean flag=false;
        private Lock lock=new ReentrantLock();
        private Condition condition_ru = lock.newCondition();
        private Condition condition_chu = lock.newCondition();

        public void setNum(String name,String sex)throws InterruptedException
        {
                lock.lock();
                try
                {
                        while(flag)
                       
                                condition_ru.await();
                        this.name=name;
                        this.sex=sex;
                        flag=true;
                        condition_chu.signal();
                       
                }
                               
               
                finally
                {
                        lock.unlock();
                }
        }
        public void out()throws InterruptedException
        {
                lock.lock();
                try
                {
                        while(!flag)
                       
                                condition_chu.await();
                                System.out.println(name+"----"+sex);
                        flag=false;
                        condition_ru.signal();
                       
                }                               
               
                finally
                {
                        lock.unlock();
                }
        }
}
class Intput implements Runnable
{
        private Res r;
        Intput(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                int x=0;
                while(true)
                {       
                        try{
                                        if(x==0)                               
                                                r.setNum("zhoubin","men");                                               
                                        else                               
                                                r.setNum("lili","women");                               
                                        x=(x+1)%2;
                                }
                                catch(InterruptedException e){}
                       
                }
        }
}

class Output implements Runnable
{
        private Res r;
        Output(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                while(true)
                {
                        try{
                                r.out();
                           }
                           catch(InterruptedException e){}
                }
        }
}

class Demo
{
        public static void main(String[] args)
                {
                       
                        Res r=new Res();
                        new Thread(new Intput(r)).start();
                        new Thread(new Output(r)).start();
                        //new Thread(new Output(r)).start();
                        //new Thread(new Output(r)).start();

                        /*
                        Intput in=new Intput(r);
                        Output ou=new Output(r);
               
                        Thread t1=new Thread(in);
                        Thread t2=new Thread(ou);
                       
                        t1.start();
                        t2.start();
                        */
                }
}



1463.tmp.png (75.9 KB, 下载次数: 29)

1463.tmp.png

作者: 小白想学javaEE    时间: 2016-4-5 02:16
                        this.name=name;
                        this.sex=sex;
                        flag=true;
                        condition_chu.signal();
这段代码写在while语句外面试试
作者: adomwon    时间: 2016-4-5 09:06
等号前后是不是都没加空格
作者: 15614014298    时间: 2016-4-5 19:43
Res.out这个方法里: lock.lock();你写成了clok.clok();

作者: zhoubinjian    时间: 2016-4-5 19:55
adomwon 发表于 2016-4-5 09:06
等号前后是不是都没加空格

这个 没有关系的
作者: zhoubinjian    时间: 2016-4-5 19:57
15614014298 发表于 2016-4-5 19:43
Res.out这个方法里: lock.lock();你写成了clok.clok();

好的,谢谢,已改,不过错误还是一样的
作者: 15614014298    时间: 2016-4-5 20:52
你导包了没有
作者: 伊乐杰    时间: 2016-4-5 22:03
看看是不是没导包
作者: 小白想学javaEE    时间: 2016-4-7 14:24
你的代码可能出现死锁
作者: 小白想学javaEE    时间: 2016-4-7 14:29
如果不满足你的循环条件,你的线程就没有等待,那么里面flag标志就不会被改变,你的线程就无休止的执行lock(); 和unlock(); 你需要执行的语句就无法被执行到
作者: zhoubinjian    时间: 2016-4-7 17:19
小白想学javaEE 发表于 2016-4-7 14:16
this.name=name;
                        this.sex=sex;
                       ...

是的,谢谢,这是个错。。
作者: zhoubinjian    时间: 2016-4-7 17:20
伊乐杰 发表于 2016-4-5 22:03
看看是不是没导包

谢谢,是没有导包,自己太粗心啦
作者: zhoubinjian    时间: 2016-4-7 17:21
15614014298 发表于 2016-4-5 20:52
你导包了没有

是没有导包。自己太粗心了
作者: BackingAlongRay    时间: 2016-4-9 19:24
导包导包




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2