11. Thread Signaling

线程信号是让线程间互相发送信号的重要手段,如等待数据处理完成。通过共享对象进行信号传递,如设置布尔变量。忙等是一种简单的方式,但CPU利用率低。Java提供了wait(), notify(), notifyAll()来让线程在等待信号时进入非活动状态。当调用wait()时,线程会释放锁,直到其他线程调用notify()或notifyAll()唤醒。需要注意的是,信号可能会丢失或发生意外的唤醒(spurious wakeups),应使用while循环防止这种情况。同时,不应在线程间共享的常量对象上使用wait()和notify(),以免信号错乱。" 126544130,14858273,Scratch编程教学:用积木绘制等边三角形,"['青少年编程', '图形绘制', 'Scratch教程']
摘要由CSDN通过智能技术生成

The purpose of thread signaling is to enable threads to send signals to each other. Additionally, thread signaling enables threads to wait for signals from other threads. For instance, a thread B might wait for a signal from thread A indicating that data is ready to be processed.

1. Signaling via Shared Objects

A simple way for threads to send signals to each other is by setting the signal values in some shared object variable. Thread A may set the boolean member variable hasDataToProcess to true from inside a synchronized block, and thread B may read the hasDataToProcess member variable, also inside a synchronized block. Here is a simple example of an object that can hold such a signal, and provide methods to set and check it:

public class MySignal{
   
  protected boolean hasDataToProcess = false;
  public synchronized boolean hasDataToProcess(){
   
    return this.hasDataToProcess;
  }
  public synchronized void setHasDataToProcess(boolean hasData){
   
    this.hasDataToProcess = hasData;  
  }
}

Thread A and B must have a reference to a shared MySignal instance for the signaling to work. If thread A and B has references to different MySignal instance, they will not detect each others signals. The data to be processed can be located in a shared buffer separate from the MySignal instance.

2. Busy Wait

Thread B which is to process the data is waiting for data to become available for processing. In other words, it is waiting for a signal from thread A which causes hasDataToProcess() to return true. Here is the loop that thread B is running in, while waiting for this signal:

protected MySignal sharedSignal = ...
...
while(!sharedSignal.hasDataToProcess()){
   
  //do nothing... busy waiting
}

Notice how the while loop keeps executing until hasDataToProcess() returns true. This is called busy waiting. The thread is busy while waiting.

3. wait(), notify() and notifyAll()

Busy waiting is not a very efficient utilization of the CPU in the computer running the waiting thread, except if the average waiting time is very small. Else, it would be smarter if the waiting thread could somehow sleep or become inactive until it receives the signal it is waiting for.
Java has a builtin wait mechanism that enable threads to become inactive while waiting for signals. The class java.lang.Object defines three methods, wait(), notify(), and notifyAll(), to facilitate this.
A thread that calls wait() on any object becomes inactive until another thread calls notify() on that object. In order to call either wait() or notify the calling thread must first obtain the lock on that object. In other words, the calling thread must call wait() or notify() from inside a synchronized block.
Here is a modified version of MySignal called MyWaitNotify that uses wait() and notify().

public class MonitorObject
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值