05 线程通信

问题

java中的线程通信机制有哪些

答案

线程同步也是线程通信的一种,例如这个线程修改了某个数据,另一个线程读取了修改后的数据,这本质上就是通信。Object类提供的wait(), notify(),notifyAll()是我们通常讲得线程通信,它们被引入的意义在于由轮询侦听变为事件触发。例如以下的代码:

import java.util.Arrays;
import java.util.Comparator;

public class Test {
    public synchronized void test1()
    {
        System.out.println(Thread.currentThread().getName()+" test1()");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                test.test1();
            }
        });

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.test1();
            }
        });
        //当thread线程正在执行test1方法的时候,thread1线程就得等着;
        //等到thread执行完毕,thread1才能执行,但是thread1怎么才能知道thread执行完毕呢,就得不断去轮询。
        //轮询的效率是比较低的,那有没有更高效率的办法呢,wait以及notify就是解决这个问题的。
        thread.start();
        thread1.start();
    }
}

当thread线程正在执行test1方法的时候,thread1线程就得等着;等到thread执行完毕,thread1才能执行,但是thread1怎么才能知道thread执行完毕呢,就得不断去轮询。轮询的效率是比较低的,那有没有更高效率的办法呢,wait以及notify就是解决这个问题的。
例如:thread1发现thread正在执行,就wait等待,等到thread执行完毕,就去notify唤醒thread1。如此一来,thread1就不用轮询查看thread是否执行完毕,效率得以提高。那么上面的代码就可以变为:

public class Test1 {


    public void test1()
    {
        while(true) {
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + " test1()");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                notify();
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Test1 test = new Test1();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                test.test1();
            }
        });

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.test1();
            }
        });
        //thread执行完毕,唤醒thread1,阻塞自己;
        //thread1执行完毕,唤醒thread,阻塞自己。
        //如此,周而复始,不断循环。
        thread.start();
        thread1.start();
    }
}

wait(), notify(), notifyAll()之所以必须写在synchronized代码块当中,是因为他们的作用就是对“多个线程对同一个资源进行操作会产生轮询”进行优化;而它们为什么写在Object类中,是因为synchronized的对象锁可以是任意对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值