前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java多线程04——线程通信

Java多线程04——线程通信

作者头像
头发还在
发布2023-10-16 10:58:19
1310
发布2023-10-16 10:58:19
举报
文章被收录于专栏:桃花源

1 线程通信机制

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

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

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

实现线程间的通信方法:

  • wait / notify

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

  • LockCondition

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

2 线程通信的 waitnotify 机制

  • 等待/通知机制

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

  • 从功能上来说,wait 就是线程在获取对象锁后,主动释放对象锁,同时本线程休眠。直到有其它线程调用对象的 notify() 唤醒该线程,才能继续获取对象锁,并继续执行。
  • wait()notify() 方法是Object的方法,即所有对象都拥有该方法。
代码语言:javascript
复制
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();
    }
}
复制代码

测试类

代码语言:javascript
复制
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 机制改写上边的类

代码语言:javascript
复制
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();
    }
}
复制代码

测试类

代码语言:javascript
复制
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 代码块,同时加锁和解锁都需要手动操作。二者略有不同。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 线程通信机制
  • 2 线程通信的 wait 和 notify 机制
  • 3 线程通信的 Lock 和 Condition 机制
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档