多线程详解-03线程通信

27 篇文章 0 订阅
3 篇文章 0 订阅

6. 线程通信

6.1

Object提供了三个方法,可通过同步监视器对象来调用:

  • wait():让当前线程等待,直到其他线程调用该同步监视器的notify()方法或notifyAll()来唤醒该线程
  • notify():唤醒在此同步监视器上等待的单个线程。
  • notifyAll():唤醒所有同步监视器上等待的单个线程。

如同操作系统中的P/V操作一般,可以利用信号量机制辅助,从而实现多个线程之间通信。

示例代码:

private boolean flag = false;
public syuchronized void draw (double draw) {
	try{
		if(flag){wait();}
		else {
			//执行操作
			……
			flag = false;
			notify();
		}
	}
	catch (InterruptedException ex){ex.printStachTrace();}
}

示例代码

/**
* 测试生产者消费者模型-->利用缓冲区解决:管程法
 * 生产者,消费者,产品,缓冲区
**/
public class TestPC {
    public static void main(String[] args) {
        BufferZone bufferZone = new BufferZone();
        new Producer(bufferZone).start();
        new Customer(bufferZone).start();
    }
}
//生产者
class Producer extends Thread {
    BufferZone bufferZone;
    public Producer(BufferZone bufferZone) {
        this.bufferZone = bufferZone;
    }
    @Override
    public void run() {
        for (int i = 1; i < 100; i++) {
            bufferZone.pro(new Product());
            System.out.println("生产了"+i+"个产品");

        }
    }
}
//消费者
class Customer extends Thread {
    BufferZone bufferZone;
    public Customer(BufferZone bufferZone) {
        this.bufferZone = bufferZone;
    }
    @Override
    public void run() {
        for (int i = 1; i < 100; i++) {
            bufferZone.cus();
            System.out.println("消费了了"+i+"个产品");
        }
    }
}

class Product {
}
class BufferZone{
    Product[] products = new Product[10];
    int num=0;
    //生产
    public synchronized void pro(Product product) {
        if(num==products.length-1){
            try {
                System.out.println("满了");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        products[num++] = product;
        this.notify();
    }
    //消费
    public synchronized void cus() {
        if(num ==0) {
            try {
                System.out.println("空了");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        num--;
        this.notify();
    }
}

6.2 Condition控制线程通信

如果程序不使用synchronized关键字来保证同步,而是直接使用Lock对象,则系统不存在隐式同步监视器,便不能使用wait()等方法进行线程通信了。

不怕,还有Condition类来保持通信。Condition与Lock关系一对一:
Condition cond = lock.newCondition()来获得Condition对象
Condition将同步监视器的(wait(),notify()、notifyAll())方法改写为

  • await() :同wait()
  • signal():同notify()
  • signalAll():同notifyAll()

用法同wait()等差不多。

6.3 通过BlockingQueue控制线程通信

首先要了解阻塞队列BlockingQueue,这部分感觉有些复杂,所以放在后面。

大体可将BlockingQueue比作一个容器:当生产者试图向BlockingQueue中放入元素时,如果该队列已满,则该线程被阻塞;当消费者试图从BlockingQueue取出元素时,如果该队列为空,则该线程被阻塞。

6.4 通过管道进行线程间通信

管道流(pipe stream)是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道中读取数据。
java JDK提供四个类来使线程间可以进行通信,

  • PipedInputStream
  • PipedOutputStream
  • PipedReader
  • PipedWrite

6.4.1 通过字节流通信

使用PipedOutputStream和PipedInputStream

6.4.2 通过字符流通信

PipedWriter和PipedReader

7.线程组

查看高手文档:线程组ThreadGroup分析详解 多线程中篇

8. ForkJoinPool线程池的使用以及原理

查看YourBatman的一篇博客:【小家java】Java线程池之—ForkJoinPool线程池的使用以及原理

9. Java 线程工具类

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值