疑问:多生产和多消费 操作值-假死 来自:《JAVA多线程编程核心技术》3.1.11.2 ...

JDK:1.8
背景:
当我测试多生产和多消费 操作值-假死时,分别两个生产和消费线程,每个线程内循环两次相应方法
问题:
打印日志 发现,消费2有一次 wait 在未唤醒时就接着往下执行了,不知道这是为什么?
先把测试代码附上
日志中始终有
[i][color=red]消费者2 WAITING 0 解除 value=null
消费者2 WAITING 1[/color][/i]
正常解除 value后面是有值的,这次却没有,虽然没影响功能,但感到很奇怪。

/**
* 3.1.11.2 多生产和多消费 操作值-假死
* Created by ironlee on 17/12/23.
*/
public class ProducerAndConsumerAllWait {

private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

/**
* 生产
* @param lock
*/
public void producer(Object lock){
synchronized (lock){
try {
int i = 0 ;
while(StringUtils.isNotBlank(getValue())) {
System.out.println(Thread.currentThread().getName()+" WAITING value="+getValue()+" "+(i++));
lock.wait();
}
String value = System.currentTimeMillis()+"-"+System.nanoTime();
System.out.println(Thread.currentThread().getName()+" RUNNABLE value="+value);
setValue(value);
lock.notify();
// System.out.println(System.currentTimeMillis()+"-"+System.nanoTime()+" 生产者 "+Thread.currentThread().getName()+" NOTIFY value="+getValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

/**
* 消费
* @param lock
*/
public void consumer(Object lock){
synchronized (lock){
try{
int i = 0 ;
// 为什么 没唤醒 wait能接着往下走?
while(StringUtils.isBlank(getValue())){
System.out.println(Thread.currentThread().getName()+" WAITING "+(i));
lock.wait();
System.out.println(Thread.currentThread().getName()+" WAITING "+(i) + " 解除 value="+getValue());
i++;

}
System.out.println(Thread.currentThread().getName()+" RUNNABLE value="+getValue());
setValue(null);
lock.notify();
// System.out.println(System.currentTimeMillis()+"-"+System.nanoTime()+" 消费者 "+Thread.currentThread().getName()+" NOTIFY value="+getValue());
}catch(InterruptedException e){
e.printStackTrace();
}
}
}

public static void main(String[] args) {
final Object lock = new Object();
final ProducerAndConsumerAllWait domain = new ProducerAndConsumerAllWait();
final int count = 2;
Runnable producer = new Runnable() {
@Override
public void run() {
synchronized (lock){
int i = 0 ;
while( i < count ){
System.out.println( Thread.currentThread().getName()+" 序号="+i);
domain.producer( lock );
i++;
}
}
}
};
Runnable consumer = new Runnable() {
@Override
public void run() {
synchronized (lock){
int i = 0 ;
while(i< count){
System.out.println( Thread.currentThread().getName()+" 序号="+i);
domain.consumer( lock );
i++;
}
}
}
};
for( int ii = 0 ; ii < 2 ; ii++){
Thread producerT = new Thread(producer);
producerT.setName("生产者"+(ii+1));
producerT.start();
Thread consumerT = new Thread(consumer);
consumerT.setName("消费者"+(ii+1));
consumerT.start();
}
try {
Thread.sleep( 1000L );
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread[] threadArray = new Thread[Thread.currentThread().getThreadGroup().activeCount()];
Thread.currentThread().getThreadGroup().enumerate( threadArray );
for( Thread t : threadArray ){
System.out.println( t.getName() + " " + t.getState() );
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值