java 多线程 生产者 消费者模型问题

ericf1987 2018-02-26 04:42:21
最近一直在温习java多线程变成方面的知识,闲来无事,写了一段消费者和生产者之间线程通信的例子,代码如下
public class TestConsumerAndProducer {


//消费者
static class Consumer implements Runnable{


private final Vector<String> obj;
public Consumer(Vector<String> v){
this.obj = v;
}


@Override
public void run() {
synchronized (obj){
while (true) {


try {
System.out.println("移除苹果:" + obj.remove(0));
System.out.println("consumer thread:" + Thread.currentThread().getName() + ", obj size : " + obj.size());
if (obj.isEmpty()) {
//消费者等待
System.out.println("-----即将进入等待队列的线程::" + Thread.currentThread().getName());
obj.wait();
System.out.println("-----即将开始运行的线程:" + Thread.currentThread().getName());
}
//唤醒生产者
obj.notify();
System.out.println("-----被唤醒的线程:" + Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(3);


} catch (Exception e) {
e.printStackTrace();
}


}
}
}
}


//生产者
static class Producer implements Runnable{


private final Vector<String> obj;


public Producer(Vector<String> v){
this.obj = v;
}


@Override
public void run() {
synchronized (obj){
do {


try {


if (obj.size() > 10) {
//生产者进行等待
System.out.println("-----即将进入等待队列的线程::" + Thread.currentThread().getName());
obj.wait();
System.out.println("-----即将开始运行的线程:" + Thread.currentThread().getName());
}
String seed = "apple" + new Random().nextInt(10);
obj.add(seed);
System.out.println("添加苹果:" + seed);
System.out.println("producer thread:" + Thread.currentThread().getName() + ", obj size : " + obj.size());
//唤醒消费者 此处在生产者没有执行wait 并释放锁之前 其他等待中的线程不会执行
obj.notify();
System.out.println("-----被唤醒的线程:" + Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}


} while (true);
}
}
}


public static void main(String[] args) {
Vector<String> obj = new Vector<>();
obj.add("apple");
obj.add("apple");


Thread consumer = new Thread(new Consumer(obj));
Thread producer = new Thread(new Producer(obj));


Thread consumer1 = new Thread(new Consumer(obj));
Thread producer1 = new Thread(new Producer(obj));
producer.setName("生产者1号");
producer1.setName("生产者2号");
consumer.setName("消费者1号");
consumer1.setName("消费者2号");


consumer.start();
producer.start();


consumer1.start();
producer1.start();


}



}

那么,问题来了。

以下是控制台日志

移除苹果:apple
consumer thread:消费者1号, obj size : 1
-----被唤醒的线程:消费者1号
移除苹果:apple
consumer thread:消费者1号, obj size : 0
-----即将进入等待队列的线程::消费者1号
添加苹果:apple0
producer thread:生产者2号, obj size : 1
-----被唤醒的线程:生产者2号
添加苹果:apple6
producer thread:生产者2号, obj size : 2
-----被唤醒的线程:生产者2号
添加苹果:apple2
producer thread:生产者2号, obj size : 3
-----被唤醒的线程:生产者2号
添加苹果:apple7
producer thread:生产者2号, obj size : 4
-----被唤醒的线程:生产者2号
添加苹果:apple2
producer thread:生产者2号, obj size : 5
-----被唤醒的线程:生产者2号
添加苹果:apple6
producer thread:生产者2号, obj size : 6
-----被唤醒的线程:生产者2号
添加苹果:apple6
producer thread:生产者2号, obj size : 7
-----被唤醒的线程:生产者2号
添加苹果:apple4
producer thread:生产者2号, obj size : 8
-----被唤醒的线程:生产者2号
添加苹果:apple6
producer thread:生产者2号, obj size : 9
-----被唤醒的线程:生产者2号
添加苹果:apple3
producer thread:生产者2号, obj size : 10
-----被唤醒的线程:生产者2号
添加苹果:apple4
producer thread:生产者2号, obj size : 11
-----被唤醒的线程:生产者2号








-----即将进入等待队列的线程::生产者2号
移除苹果:apple0
consumer thread:消费者2号, obj size : 10
-----被唤醒的线程:消费者2号
移除苹果:apple6
consumer thread:消费者2号, obj size : 9
-----被唤醒的线程:消费者2号
移除苹果:apple2
consumer thread:消费者2号, obj size : 8
-----被唤醒的线程:消费者2号
移除苹果:apple7
consumer thread:消费者2号, obj size : 7
-----被唤醒的线程:消费者2号
移除苹果:apple2
consumer thread:消费者2号, obj size : 6
-----被唤醒的线程:消费者2号
移除苹果:apple6
consumer thread:消费者2号, obj size : 5
-----被唤醒的线程:消费者2号
移除苹果:apple6
consumer thread:消费者2号, obj size : 4
-----被唤醒的线程:消费者2号
移除苹果:apple4
consumer thread:消费者2号, obj size : 3
-----被唤醒的线程:消费者2号
移除苹果:apple6
consumer thread:消费者2号, obj size : 2
-----被唤醒的线程:消费者2号
移除苹果:apple3
consumer thread:消费者2号, obj size : 1
-----被唤醒的线程:消费者2号


移除苹果:apple4
consumer thread:消费者2号, obj size : 0
-----即将进入等待队列的线程::消费者2号
添加苹果:apple1
producer thread:生产者1号, obj size : 1
-----被唤醒的线程:生产者1号
添加苹果:apple6
producer thread:生产者1号, obj size : 2
-----被唤醒的线程:生产者1号
添加苹果:apple9
producer thread:生产者1号, obj size : 3
-----被唤醒的线程:生产者1号
添加苹果:apple8
producer thread:生产者1号, obj size : 4
-----被唤醒的线程:生产者1号
添加苹果:apple5
producer thread:生产者1号, obj size : 5
-----被唤醒的线程:生产者1号
添加苹果:apple3
producer thread:生产者1号, obj size : 6
-----被唤醒的线程:生产者1号
添加苹果:apple3
producer thread:生产者1号, obj size : 7
-----被唤醒的线程:生产者1号
添加苹果:apple8
producer thread:生产者1号, obj size : 8
-----被唤醒的线程:生产者1号
添加苹果:apple6
producer thread:生产者1号, obj size : 9
-----被唤醒的线程:生产者1号
添加苹果:apple9
producer thread:生产者1号, obj size : 10
-----被唤醒的线程:生产者1号
添加苹果:apple0
producer thread:生产者1号, obj size : 11
-----被唤醒的线程:生产者1号








-----即将进入等待队列的线程::生产者1号
-----即将开始运行的线程:消费者2号
-----被唤醒的线程:消费者2号
移除苹果:apple1
consumer thread:消费者2号, obj size : 10
-----被唤醒的线程:消费者2号
移除苹果:apple6
consumer thread:消费者2号, obj size : 9
-----被唤醒的线程:消费者2号
移除苹果:apple9
consumer thread:消费者2号, obj size : 8
-----被唤醒的线程:消费者2号
移除苹果:apple8
consumer thread:消费者2号, obj size : 7
-----被唤醒的线程:消费者2号
移除苹果:apple5
consumer thread:消费者2号, obj size : 6
-----被唤醒的线程:消费者2号
移除苹果:apple3
consumer thread:消费者2号, obj size : 5
-----被唤醒的线程:消费者2号
移除苹果:apple3
consumer thread:消费者2号, obj size : 4
-----被唤醒的线程:消费者2号
移除苹果:apple8
consumer thread:消费者2号, obj size : 3
-----被唤醒的线程:消费者2号
移除苹果:apple6
consumer thread:消费者2号, obj size : 2
-----被唤醒的线程:消费者2号
移除苹果:apple9
consumer thread:消费者2号, obj size : 1
-----被唤醒的线程:消费者2号
移除苹果:apple0
consumer thread:消费者2号, obj size : 0
-----即将进入等待队列的线程::消费者2号
-----即将开始运行的线程:生产者1号
添加苹果:apple4
producer thread:生产者1号, obj size : 1
-----被唤醒的线程:生产者1号










添加苹果:apple4
producer thread:生产者1号, obj size : 2
-----被唤醒的线程:生产者1号
添加苹果:apple7
producer thread:生产者1号, obj size : 3
-----被唤醒的线程:生产者1号
添加苹果:apple3
producer thread:生产者1号, obj size : 4
-----被唤醒的线程:生产者1号
添加苹果:apple0
producer thread:生产者1号, obj size : 5
-----被唤醒的线程:生产者1号
添加苹果:apple2
producer thread:生产者1号, obj size : 6
-----被唤醒的线程:生产者1号
添加苹果:apple7
producer thread:生产者1号, obj size : 7
-----被唤醒的线程:生产者1号
添加苹果:apple3
producer thread:生产者1号, obj size : 8
-----被唤醒的线程:生产者1号
添加苹果:apple4
producer thread:生产者1号, obj size : 9
-----被唤醒的线程:生产者1号
添加苹果:apple6
producer thread:生产者1号, obj size : 10
-----被唤醒的线程:生产者1号
添加苹果:apple0
producer thread:生产者1号, obj size : 11
-----被唤醒的线程:生产者1号












-----即将进入等待队列的线程::生产者1号
-----即将开始运行的线程:消费者2号
-----被唤醒的线程:消费者2号






移除苹果:apple4
consumer thread:消费者2号, obj size : 10
-----被唤醒的线程:消费者2号
移除苹果:apple4
consumer thread:消费者2号, obj size : 9
-----被唤醒的线程:消费者2号
移除苹果:apple7
consumer thread:消费者2号, obj size : 8
-----被唤醒的线程:消费者2号
移除苹果:apple3
consumer thread:消费者2号, obj size : 7
-----被唤醒的线程:消费者2号
移除苹果:apple0
consumer thread:消费者2号, obj size : 6
-----被唤醒的线程:消费者2号
移除苹果:apple2
consumer thread:消费者2号, obj size : 5
-----被唤醒的线程:消费者2号
移除苹果:apple7
consumer thread:消费者2号, obj size : 4
-----被唤醒的线程:消费者2号
移除苹果:apple3
consumer thread:消费者2号, obj size : 3
-----被唤醒的线程:消费者2号
移除苹果:apple4
consumer thread:消费者2号, obj size : 2
-----被唤醒的线程:消费者2号
移除苹果:apple6
consumer thread:消费者2号, obj size : 1
-----被唤醒的线程:消费者2号
移除苹果:apple0
consumer thread:消费者2号, obj size : 0
-----即将进入等待队列的线程::消费者2号
-----即将开始运行的线程:生产者1号

在main方法中,我同时启用了 生产者1号,生产者2号,消费者1号,消费者2号四个线程,同时对一个Vector对象进行操作,

可是从日志中可以得知,最先进入工作的是消费者1号,生产者2号,在两者执行完后,后续则完全由生产者1号和消费者2号执行。重复运行main方法可得到相同的结论,最先执行的消费者和生产者,只能执行一次,后续则完全由其他生产者和消费者不断重复地执行,是何原因?还请各位专家们指点,感激不尽!
...全文
708 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
oO临时工Oo 2018-02-27
  • 打赏
  • 举报
回复
The choice is arbitrary and occurs at the discretion of the implementation 选择notify的线程的任意的,这里的任意,我的理解是JVM可以用任意的选择算法,包括随机或队列。 至于你所说的每次唤醒的都是同一对线程,可能只是个巧合。不管唤醒谁,其实没有实际的影响,线程不是人,不会累死。
 /**
     * Wakes up a single thread that is waiting on this object's
     * monitor. If any threads are waiting on this object, one of them
     * is chosen to be awakened. The choice is arbitrary and occurs at
     * the discretion of the implementation. A thread waits on an object's
     * monitor by calling one of the {@code wait} methods.
     * <p>
     * The awakened thread will not be able to proceed until the current
     * thread relinquishes the lock on this object. The awakened thread will
     * compete in the usual manner with any other threads that might be
     * actively competing to synchronize on this object; for example, the
     * awakened thread enjoys no reliable privilege or disadvantage in being
     * the next thread to lock this object.
     * <p>
     * This method should only be called by a thread that is the owner
     * of this object's monitor. A thread becomes the owner of the
     * object's monitor in one of three ways:
     * <ul>
     * <li>By executing a synchronized instance method of that object.
     * <li>By executing the body of a {@code synchronized} statement
     *     that synchronizes on the object.
     * <li>For objects of type {@code Class,} by executing a
     *     synchronized static method of that class.
     * </ul>
     * <p>
     * Only one thread at a time can own an object's monitor.
     *
     * @exception  IllegalMonitorStateException  if the current thread is not
     *               the owner of this object's monitor.
     * @see        java.lang.Object#notifyAll()
     * @see        java.lang.Object#wait()
     */
    public final native void notify();
ericf1987 2018-02-27
  • 打赏
  • 举报
回复
引用 2 楼 oyljerry 的回复:
用notifyall通知所有线程来竞争锁。
刚才看了一下API 确实 但是仍有一事不解,为何用notify 唤醒的永远是同一个线程呢?
ericf1987 2018-02-27
  • 打赏
  • 举报
回复
引用 2 楼 oyljerry 的回复:
用notifyall通知所有线程来竞争锁。
您好,多个占用对象锁的线程调用wait()方法后,都会进入等待队列,这个时候,如果有一个线程调用notify方法,是唤醒前述多个线程中的同一个 还是能够随机唤醒多个呢?是不是也是按照队列先进先出的规则进行唤醒的?
oO临时工Oo 2018-02-27
  • 打赏
  • 举报
回复
弄懂了记得结帖
ericf1987 2018-02-27
  • 打赏
  • 举报
回复
引用 5 楼 trocp 的回复:
The choice is arbitrary and occurs at the discretion of the implementation 选择notify的线程的任意的,这里的任意,我的理解是JVM可以用任意的选择算法,包括随机或队列。 至于你所说的每次唤醒的都是同一对线程,可能只是个巧合。不管唤醒谁,其实没有实际的影响,线程不是人,不会累死。
 /**
     * Wakes up a single thread that is waiting on this object's
     * monitor. If any threads are waiting on this object, one of them
     * is chosen to be awakened. The choice is arbitrary and occurs at
     * the discretion of the implementation. A thread waits on an object's
     * monitor by calling one of the {@code wait} methods.
     * <p>
     * The awakened thread will not be able to proceed until the current
     * thread relinquishes the lock on this object. The awakened thread will
     * compete in the usual manner with any other threads that might be
     * actively competing to synchronize on this object; for example, the
     * awakened thread enjoys no reliable privilege or disadvantage in being
     * the next thread to lock this object.
     * <p>
     * This method should only be called by a thread that is the owner
     * of this object's monitor. A thread becomes the owner of the
     * object's monitor in one of three ways:
     * <ul>
     * <li>By executing a synchronized instance method of that object.
     * <li>By executing the body of a {@code synchronized} statement
     *     that synchronizes on the object.
     * <li>For objects of type {@code Class,} by executing a
     *     synchronized static method of that class.
     * </ul>
     * <p>
     * Only one thread at a time can own an object's monitor.
     *
     * @exception  IllegalMonitorStateException  if the current thread is not
     *               the owner of this object's monitor.
     * @see        java.lang.Object#notifyAll()
     * @see        java.lang.Object#wait()
     */
    public final native void notify();
再次感谢!
oyljerry 2018-02-26
  • 打赏
  • 举报
回复
用notifyall通知所有线程来竞争锁。
oO临时工Oo 2018-02-26
  • 打赏
  • 举报
回复
应该用obj.notifyAll(); obj.notify();一次只能唤醒一个等待的线程

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧