线程中的等待唤醒机制

  1.   程间的通讯:其实就是多个线程在操作同一资源,但是操作的动作不同
  2.  第一步:考虑同步

              第二步:共享的代码都需要同步

              第三步:同步代码快(里面对象的选择)

  1. 等待唤醒机制:Wait()和 notify() notifyAll()的时候必须在try catch包起来 ,都使用在同步中,因为要对监视器(锁)的线程操作,所以要使用在同步中,因为只有同步才具有锁

另外,这些线程的方法都必须定义在object类中, 因为这些方法在操作同步中线程,可以被同一个锁上的notify唤醒。不可以对不同锁中的线程进行唤醒。

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

而锁可以是任意的对象,所以可以被任意对象调用的方法定义在object类中

 

例子:

package sxy;

class Res{
    String name;
    String sex;
    boolean flag = false;
}

class Input implements Runnable{
    private Res r;
    Input(Res r){
        this.r=r;
    }
    public void run(){
        int x=0;
        while(true){
            synchronized (r) {
                if(r.flag){
                    try {
                        r.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(x==0){
                    r.name="Tom";
                    r.sex="man";
                }else{
                    r.name="丽丽";
                    r.sex="女女";
                }
                x = (x+1)%2;
                r.flag=true;
                r.notify();
            }
        }
    }
}
class Output implements Runnable{
    private Res r;
    Output(Res r){
        this.r=r;
    }
    public void run(){
        while(true){
            synchronized (r) {
                if(!r.flag)
                    try {
                        r.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                System.out.println(r.name+"---"+r.sex);
                r.flag = false;
                r.notify();
            }
        }
        
    }
}

public class Information {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Res r = new Res();
        
        Input in = new Input(r);
        Output out = new Output(r);
        
        Thread t1 = new Thread(in);
        Thread t2 = new Thread(out);
        
        t1.start();
        t2.start();

    }

}

 

 

代码优化:

package sxy;

class Res {
    private String name;
    private String sex;
    private boolean flag = false;

    public synchronized void set(String name, String sex) {
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        this.name = name;
        this.sex = sex;
        flag = true;
        this.notify();
    }

    public synchronized void out() {
        if (!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println(name + "----" + "sex");
        flag = false;
        this.notify();
    }
}

class Input implements Runnable {
    private Res r;

    Input(Res r) {
        this.r = r;
    }

    public void run() {
        int x = 0;
        while (true) {
            if (x == 0) {
                r.set("Tom", "man");
            } else {
                r.set("丽丽", "女女");
            }
            x = (x + 1) % 2;
        }
    }
}

class Output implements Runnable {
    private Res r;

    Output(Res r) {
        this.r = r;
    }

    public void run() {
        while (true) {
            r.out();
        }
    }

}

public class Information {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Res r = new Res();

        new Thread(new Input(r)).start();
        new Thread(new Output(r)).start();

        /*
         * Input in = new Input(r); Output out = new Output(r);
         * 
         * Thread t1 = new Thread(in); Thread t2 = new Thread(out);
         * 
         * t1.start(); t2.start();
         */

    }

}

 

posted @ 2018-03-18 19:38  helloWorldhelloWorld  阅读(170)  评论(0)    收藏  举报