线程通信

两个不同线程操作同一个资源 使用同步来完成,锁使用共享的资源对象,但会造成线程阻塞,同一时间段只会输出同一输入,无法做到输出当时输入的

线程间通信的方法 wait():调用了 wait()方法的线程进入等待池进行等待,等待 池中的线程不去竞争对象锁,直到其它的线程通知,才会进入 锁池 notify():随机唤醒一个在该对象上等待的线 程,被唤醒的线 程进行锁池,开始竞争该对锁上的锁 notifyAll():唤醒所有在该对象上等待的线程 优先级高的线程有可能先竞争到对象锁 只能在同步方法和同步代码块中使用

//Resource
public class Resource {
    String name;
    String sex;
}
//Input
public class Input implements Runnable{
    Resource r;

    public Input(Resource r) {
        this.r = r;
    }

    @Override
    public void run() {
        int x=0;
        while(true){
            synchronized (r){
                if(x==0){
                    r.name="yk";
                    r.sex="male";
                }else{
                    r.name="kk";
                    r.sex="female";
                }
            }
            x=(x+1)%2;
        }
    }
}
//Output
public class Output implements Runnable{
    Resource r;
    Output(Resource r){
        this.r = r;
    }

    @Override
    public void run() {
        while(true){
            synchronized (r){
                System.out.println(r.name+"...."+r.sex);
            }
        }
    }
}
//main
Resource r = new Resource();
Input in = new Input(r);
Output ot = new Output(r);

Thread tInput = new Thread(in);
Thread tOutput = new Thread(ot);

tInput.start();
tOutput.start();

等待唤醒

  • wait():让线程处于冻结状态,释放CPU执行权和执行资格,会被存储到线程池中
  • notify():唤醒线程池中任意一个线程,具备执行资格
  • notifyAll():唤醒线程池中所有线程

这些方法必须在同步代码中,因为这些方法是用于操作线程状态的方法,必须要明确到底操作的是哪个锁上的线程 都是定义在Object中,所以要用锁对象来调用,作为监视器

//Resource
public class Resource {
    String name;
    String sex;
    boolean flag=false;
}
//Input
public class Input implements Runnable{
    Resource r;

    public Input(Resource r) {
        this.r = r;
    }

    @Override
    public void run() {
        int x=0;
        while(true){
            synchronized (r){
                if(r.flag){
                    try {
                        r.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(x==0){
                    r.name="yk";
                    r.sex="male";
                }else{
                    r.name="kk";
                    r.sex="female";
                }
                r.flag=true;
                r.notify();
            }
            x=(x+1)%2;
        }
    }
}
//Output
public class Output implements Runnable{
    Resource r;
    Output(Resource r){
        this.r = r;
    }

    @Override
    public void run() {
        while(true){
            synchronized (r){
                if(!r.flag){
                    try {
                        r.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(r.name+"...."+r.sex);
                r.flag=false;
                r.notify();
            }
        }
    }
}
//main
Resource r = new Resource();
Input in = new Input(r);
Output ot = new Output(r);

Thread tInput = new Thread(in);
Thread tOutput = new Thread(ot);

tInput.start();
tOutput.start();

代码优化,输入,输出函数放入对象中

//Resource
public class Resource {
    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) {
                e.printStackTrace();
            }
        }
        this.name = name;
        this.sex = sex;
        flag=true;
        this.notify();
    }
    public synchronized void out(){
        if(!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name+"...."+sex);
        flag=false;
        this.notify();
    }


}
//Input
public class Input implements Runnable{
    Resource r;

    public Input(Resource r) {
        this.r = r;
    }

    @Override
    public void run() {
        int x=0;
        while(true){
            synchronized (r){
                if(x==0){
                    r.set("yk","male");
                }else{
                    r.set("kk","female");
                }
            }
            x=(x+1)%2;
        }
    }
}
//Output
public class Output implements Runnable{
    Resource r;
    Output(Resource r){
        this.r = r;
    }

    @Override
    public void run() {
        while(true){
            r.out();
        }
    }
}
//main
Resource r = new Resource();
Input in = new Input(r);
Output ot = new Output(r);

Thread tInput = new Thread(in);
Thread tOutput = new Thread(ot);

tInput.start();
tOutput.start();


书籍推荐