多线程之------线程通信的几种方式

1.第一种方式是常见的我们叫它等待唤醒方式吧

public class TestThreadCommunicate {
    public static void main(String[] args) {
        Communicate communicate = new Communicate();
        new Thread(new Runnable() {

            @Override
            public void run(){
                for (int i = 1; i <= 50; i++) {
                    communicate.sub();
                }
            }

        }).start();
        for (int i = 1; i <= 10; i++) {
            communicate.main();
        }
    }
}
class Communicate{

    private boolean bool = true;

    public synchronized void sub() {
        while (!bool) {
            try {
                // 满足条件那么就等待,所以先不满足条件,等执行完后让子线程等待,主线程执行
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int j = 1; j <= 10; j++) {
            System.out.println("childThread......");
        }
        bool = false;
        // 这个唤醒的是下一个线程
        this.notify();
    }

    public synchronized void main() {
        while (bool) {
            try {
                // 当执行完成后让主线程等,子线程执行
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int j = 1; j <= 50; j++) {
            System.out.println("mainThread......");
        }
        bool = true;
        this.notify();
    }
}

代码执行过程:先让子线程执行,让主线程等待,等子线程执行完,让主线程执行,子线程等待。

2.使用Condition的方式

public class Condition {
    public static void main(String[] args) {
        Communicate communicate = new Communicate();
        new Thread(new Runnable() {

            @Override
            public void run(){
                for (int i = 1; i <= 50; i++) {
                    communicate.sub();
                }
            }

        }).start();
        for (int i = 1; i <= 10; i++) {
            communicate.main();
        }
    }
  static class Communicate{

        private boolean bool = true;
        Lock lock = new ReentrantLock();
        java.util.concurrent.locks.Condition condition = lock.newCondition();
        public void sub() {
            lock.lock();
            try {
                while (!bool) {
                    try {
                        // 满足条件那么就等待,所以先不满足条件,等执行完后让子线程等待,主线程执行
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int j = 1; j <= 10; j++) {
                    System.out.println("childThread......");
                }
                bool = false;
                condition.signal();
            }finally {
                lock.unlock();
            }
        }

        public void main() {
            lock.lock();
            try {
                while (bool) {
                    try {
                        // 当执行完成后让主线程等,子线程执行
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int j = 1; j <= 50; j++) {
                    System.out.println("mainThread......");
                }
                bool = true;
                condition.signal();
            }finally {
                lock.unlock();
            }
            // this.notify();
        }
    }

}

这个是加锁机制,访问之前加锁,就如同第一个方式的wait。

当然还有其它的方式,自己看吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值