`

黑马程序员——多线程之间的通信

 
阅读更多

                ------- android培训java培训、期待与您交流! ----------

  • 线程间的通信

 案例图  



 

总结:

1.在存东西后,立即唤醒输出线程,让它来取

2.在取到东西后,立即唤醒输入线程,让它赶快存东西

 

线程间的通信:

 其实就是多个线程操作同一资源,但是操作的动作不同

 

wait()  notify()  notifyAll()Object类的方法

都使用在同步中,所用需要用监视器(锁)的线程操作

所以要使用在同步中,因为只有同步才有锁

为什么这些方法都要定义在Object中?

只用同一个线程上的被等待线程,可以被同一个锁上的notify唤醒

也就是说:等待和唤醒必须是同一把锁

锁可以是任意对象,所以这些方法定义在object

 案例代码:

class Res{  //用于存储共享数据(共享资源)
    String name;
    String sex;  
    boolean flag=false;
}
class Input implements Runnable{
    private Res r;
    public Input(Res r)
    {
        this.r=r;
    }
    public void run(){
        int x=0;
        r.flag=false;
        while(true)
        {
            synchronized (r) 
            {
                if(r.flag)//判断flag==true,则让input的线程状态变为:wait()
                {
                   try {
                        r.wait();
                    }
                     catch (Exception e)
                    {
                        System.out.println(e.toString());
                    }    
                }
                  //flag==false 执行input操作
                if(x==0)
                {
                    r.name="zhangsan";
                    r.sex="man";
                }
                else 
                {
                    r.name="李四";
                    r.sex="女女---";
                }
                x=(x+1)%2;    
                r.flag=true;
                try {     //将ouput(输出线程)状态唤醒
                    r.notify();
                } catch (Exception e) {
                    // TODO: handle exception
                }                                
            }            
        }
    }
}
class Output implements Runnable{
    private Res r;
    boolean flag=false;
    public Output(Res r) {
        this.r=r;
    }
    public void run(){
        while(true)
        { 
            synchronized (r) 
            {
                if(r.flag)
                {
                    System.out.println(r.name+"----"+r.sex);
                    flag=false;
                    try {   //将input线程唤醒,存东西
                        r.notify();//将线程池中的线程唤醒
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
                //r.flag==false
                try {//把自己(输出线程)状态变为  wait()
                    r.wait();
                } catch (Exception e) {
                    // TODO: handle exception
                }                                    
            }            
        }        
    }
}
public class ThreadDemo {
    public static void main(String[] args) {
         Res res=new Res();
         Input input=new Input(res);
         Output output=new Output(res);
         Thread thread=new Thread(input);
         Thread thread02=new Thread(output);
         thread.start();
         thread02.start();
    }
}

 

  

-------------代码优化  start---------------------

class Res{  //用于存储共享数据(共享资源)
    private String name;
    private String sex;  
    private boolean flag=false;
    public synchronized void set(String name,String sex)
    {
        if(flag)
            把自己的状态变为:wait()跳到output执行
            try{this.wait();}catch(Exception e){}            
        this.name=name;
        this.sex=sex;
        flag=true;
        try{this.notify();}catch(Exception e){}              
    }
    public synchronized void out()
    {
        if(flag)
           System.out.println(this.name+"---"+this.sex);
           flag=false;
           //将input唤醒
           try{this.notify();}catch(Exception e){}
         //把自己的状态变为:wait()跳到input执行 
       try{this.wait();}catch(Exception e){}
    }
}
class Input implements Runnable{
    private Res res;
    Input(Res res)
    {
        this.res=res;
    }
    public void run(){
        int x=0;
        while(true)
        { 
            if(x==0)
                res.set("zhangsan", "man");             
            else 
                res.set("王丽", "女");
            x=(x+1)%2;  
        }
    }
}
class Output implements Runnable{
    private Res r;
    boolean flag=false;
    public Output(Res r) {
        this.r=r;
    }
    public void run(){
        while(true)
        { 
             r.out();
        }        
    }
}
public class ThreadDemo {
    public static void main(String[] args) {
         Res res=new Res();
         new Thread(new Input(res)).start();
         new Thread(new Output(res)).start();
    }
}

 -------------代码优化  end---------------------

  • 大小: 90.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics