Java多线程04——线程通信

1 线程通信机制

线程通信指的是不同线程之间可以交换一些实时的数据信息。

线程是操作系统中的独立个体,但这些个体如果不经过特殊处理就不能成为一个整体,线程间的通信就成为整体的必用方式之一。例如之前处理的线程同步,就是一种线程间通信的方式。

当线程存在通信指挥,系统间的交互性会更强大,在提高CPU利用率的同时,还会使开发人员对线程任务在处理过程中进行有效的把控与监督。

实现线程间的通信方法:

  • wait / notify

这两个方法都是Object类的方法,换句话说,Java为所有的对象都提供了这两个方法。

  • LockCondition

从JAVA5开始,提供了 Lock 机制,同时还有用于处理 Lock 机制通信控制的 Condition 接口。

2 线程通信的 waitnotify 机制

  • 等待/通知机制

是指线程A调用了对象的 wait() 方法进入到等待状态,而线程B调用了对象的 notify()notifyAll() 方法,线程A收到通知后退出等待队列,进入到可运行状态,进而执行后续操作。

  • 从功能上来说,wait 就是线程在获取对象锁后,主动释放对象锁,同时本线程休眠。直到有其它线程调用对象的 notify() 唤醒该线程,才能继续获取对象锁,并继续执行。
  • wait()notify() 方法是Object的方法,即所有对象都拥有该方法。
public class PrintChar {
    private boolean flag = false;

    public void printA(){
        synchronized (this) {
            while (true) {
                if(flag){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 输出A");
                flag = true;
                this.notify();
            }
        }
    }

    public void printB(){
        synchronized (this) {
            while (true) {
                if(!flag){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 输出B");
                flag = false;
                this.notify();
            }
        }
    }
}

class Thread1 extends Thread {
    PrintChar pc;

    public Thread1(PrintChar pc){
        this.pc = pc;
    }

    @Override
    public void run() {
        pc.printA();
    }
}

class Thread2 extends Thread {
    PrintChar pc;

    public Thread2(PrintChar pc){
        this.pc = pc;
    }

    @Override
    public void run() {
        pc.printB();
    }
}
复制代码

测试类

public class TestWait {
    public static void main(String[] args) {
        PrintChar pc = new PrintChar();
        new Thread1(pc).start();
        new Thread2(pc).start();
    }
}
复制代码

输出效果如下:

Thread-0 输出A

Thread-1 输出B

Thread-0 输出A

Thread-1 输出B

Thread-0 输出A

Thread-1 输出B

Thread-0 输出A

Thread-1 输出B

Thread-0 输出A

Thread-1 输出B

过程如下图所示:

 

3 线程通信的 LockCondition 机制

Lock 用于控制多线程对需要竞争的共享资源的顺序访问,保证该状态的连续性。

Condition 是 Java 提供的来实现等待/通知的类,Condition 对象是由 Lock 对象所创建。

Condition 中的 await() 方法相当于 Object 的 wait() 方法,Condition 中的 signal() 方法相当于 Object 的 notify() 方法。

使用 LockCondition 机制改写上边的类

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintChar2 {
    private boolean flag = false;
    //创建锁
    Lock lock = new ReentrantLock();
    //创建监视器
    Condition condition = lock.newCondition();

    public void printA(){

        while (true) {
            lock.lock();
            if(flag){
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " 输出A");
            flag = true;
            condition.signal();
            lock.unlock();
        }
    }

    public void printB(){

            while (true) {
                lock.lock();
                if(!flag){
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 输出B");
                flag = false;
                condition.signal();
                lock.unlock();
            }

    }
}

class Thread11 extends Thread {
    PrintChar2 pc;

    public Thread11(PrintChar2 pc){
        this.pc = pc;
    }

    @Override
    public void run() {
        pc.printA();
    }
}

class Thread22 extends Thread {
    PrintChar2 pc;

    public Thread22(PrintChar2 pc){
        this.pc = pc;
    }

    @Override
    public void run() {
        pc.printB();
    }
}
复制代码

测试类

public class TestWait {
    public static void main(String[] args) {
        PrintChar2 pc = new PrintChar2();
        new Thread11(pc).start();
        new Thread22(pc).start();
    }
}
复制代码

输出效果如下:

Thread-0 输出A

Thread-1 输出B

Thread-0 输出A

Thread-1 输出B

Thread-0 输出A

Thread-1 输出B

Thread-0 输出A

Thread-1 输出B

Thread-0 输出A

Thread-1 输出B

需要注意的是: 如果使用 Lock 实现锁机制,需要替换原来的 synchronized 代码块,同时加锁和解锁都需要手动操作。二者略有不同。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我有健康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值