标签:stop star 破坏 不一致 suspend array int OLE consumer
可以保证数据的安全,将决定权留给被中断的线程
请求方:需要发出interrput请求
被停止方:需要对interrupt作出响应,在可能抛出InterruptedException的地方作出处理
子方法:优先在子方法抛出InterruptedException,或者在收到中断信后再次设为中断状态
/**
* 用volatile的局限
*
*
* 此例中,生产者的生产速度很快,
* 消费者消费速度慢,
* 所以阻塞队列满了以后,
* 生产者会阻塞,等待消费者进一步消费
* 线程会在storage.put(num)陷入阻塞,while的判断并不能起作用
*/
public class WrongWayVolatileCantStop {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue storage = new ArrayBlockingQueue(10);
Producer producer = new Producer(storage);
Thread producerThread = new Thread(producer);
producerThread.start();
Thread.sleep(1000);
Consumer consumer = new Consumer(storage);
while (consumer.needMoreNums()) {
System.out.println(consumer.storage.take()+"被消费了");
Thread.sleep(100);
}
System.out.println("消费者不需要更多数据了。");
//一旦消费不需要更多数据了,我们应该让生产者也停下来,但是实际情况
producer.canceled=true;
System.out.println(producer.canceled);
}
}
class Producer implements Runnable {
public volatile boolean canceled = false;
BlockingQueue storage;
public Producer(BlockingQueue storage) {
this.storage = storage;
}
@Override
public void run() {
int num = 0;
try {
while (num <= 100000 && !canceled) {
if (num % 100 == 0) {
storage.put(num);
System.out.println(num + "是100的倍数,被放到仓库中了。");
}
num++;
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("生产者结束运行");
}
}
}
class Consumer {
BlockingQueue storage;
public Consumer(BlockingQueue storage) {
this.storage = storage;
}
public boolean needMoreNums() {
if (Math.random() > 0.95) {
return false;
}
return true;
}
}
标签:stop star 破坏 不一致 suspend array int OLE consumer
原文地址:https://www.cnblogs.com/guofx/p/13034707.html