背景:
当我测试多生产和多消费 操作值-假死时,分别两个生产和消费线程,每个线程内循环两次相应方法
问题:
打印日志 发现,消费2有一次 wait 在未唤醒时就接着往下执行了,不知道这是为什么?
先把测试代码附上
日志中始终有
消费者2 WAITING 0 解除 value=null
消费者2 WAITING 1
正常解除 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() ); } } }