您的位置:首页 > 编程语言 > Java开发

java 多线程等待、唤醒机制实例

2016-10-26 12:46 239 查看
例子:

1、实体类

public class Student {

    String name;

    int age;

    boolean flag = false; // 表示没有值

}

2、线程1

public class SetThread implements Runnable {

    private Student s;

    private int x = 0;

    public SetThread(Student s) {

        this.s = s;

    }

    @Override

    public void run() {

        while (true) {

            synchronized (s) {

                // 判断,如果有值,就等待

                if (s.flag) {

                    try {

                        s.wait(); //t1就等待在这里了。

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

                // 设置值

                if (x % 2 == 0) {

                    s.name = "林青霞";

                    s.age = 27;

                } else {

                    s.name = "刘意";

                    s.age = 30;

                }

                x++; //x=1,x=2,

                // 有值后,就修改标记

                s.flag = true;

                s.notify(); // 唤醒等待的线程,唤醒其他的线程,不代表其他的线程能够立即执行。

            }

            //可能t1抢到,也可能t2抢到

        }

    }

}

3、线程2

public class GetThread implements Runnable {

    private Student s;

    public GetThread(Student s) {

        this.s = s;

    }

    @Override

    public void run() {

        while (true) {

            synchronized (s) {

                // 判断没有值,就等待

                if (!s.flag) {

                    try {

                        s.wait(); //t2在这等待了。

                                  //t2线程在这里等待,那么,它就会释放锁对象。

                                  //将来,当它再次获取到执行权的时候,是从哪里等待,哪里醒来。

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

                System.out.println(s.name + "---" + s.age);

                //林青霞---27

                // 修改标记

                s.flag = false;

                s.notify(); //唤醒其他的线程

            }

            //可能t2,可能t1

        }

    }

}

4、测试代码

/*

 * 需求:我有一个学生,我可以对其属性设置值,我也可以获取其属性值。请用线程间通信的案例来体现。

 *

 * 资源:Student

 * 设置线程:SetThread

 * 获取线程:GetThread

 * 测试类:StudentDemo

 *

 * 刚才的数据是大片大片出现的。我想把这个动作改进为依次出现的数据。

 * 怎么办呢?

 *         原理:如果我的学生属性没有值,我应该先赋值,然后才能使用。

 *              如果学生的属性有值,应该使用后再赋值。

 *

 * 为了配合这个操作,java提供了一种机制:等待唤醒机制。

 *         notify()

 *         wait()

 *         这两个方法的调用应该通过锁对象去使用。

 */

public class StudentDemo {

    public static void main(String[] args) {

        // 创建一个资源

        Student s = new Student();

        SetThread st = new SetThread(s);

        GetThread gt = new GetThread(s);

        Thread t1 = new Thread(st);

        Thread t2 = new Thread(gt);

        t1.start();

        t2.start();

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐