Java 阻塞 Q 测试停止执行

Java Blocking Q test stops executing

我有以下用于学习目的的测试代码,其中我试图 运行 一个生产者和一个消费者线程 运行 在一个阻塞的 Q 上无休止地连接。

出于某种我无法理解的原因,输出如下:

Produced 3001
Q puts 3001
put: Q size = 1
Produced 3002
Q puts 3002
put: Q size = 2
Q takes 3001
take: Q size = 1
Consumed 3001

代码如下:

@Getter @Setter @NoArgsConstructor
public class MyBlockingQ {

    public BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);


    public Integer take() {
        try {
            Integer i = queue.take();
            System.out.println("Q takes " + i);
            System.out.println("take: Q size = " + queue.size());
            return i;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void put(Integer produce) {
        try {
            System.out.println("Q puts " + produce);
            queue.put(produce);
            System.out.println("put: Q size = " + queue.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class MyProducer implements Runnable {

    private final MyBlockingQ queue;
    private Integer i = 3000;

    public MyProducer(MyBlockingQ q) {
        queue = q;
    }

    public void run() {
      while (true) { 
          queue.put(produce()); 
          try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
      }
    }

    Integer produce() {
        i++;
        System.out.println("Produced " + i);
        return i;
    }

}

public class MyConsumer implements Runnable {

  private final MyBlockingQ queue;

  public MyConsumer(MyBlockingQ q) {
    queue = q;
  }

  public void run() {
    
    while (true) {

      consume(queue.take());
      
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

    }
  }

  void consume(Integer x) {

    System.out.println("Consumed " + x);

  }
}

    @Test
    public void testBlockingQ(){

        MyBlockingQ q = new MyBlockingQ();
        MyProducer p1 = new MyProducer(q);
        MyConsumer c1 = new MyConsumer(q);

        new Thread(p1).start();

        try {
            Thread.sleep(3000);
          } catch (InterruptedException e) {
            e.printStackTrace();
        }        

        new Thread(c1).start();
    }

我不明白为什么代码在最后一行输出后停止执行,如上所示? 我在最新 VSCode.

上使用 JavaSE-15

当您的测试方法结束时,线程终止。如果您希望您的线程 运行 更长,您需要在测试方法结束时添加更多的休眠时间。

@Test
public void testBlockingQ(){

    MyBlockingQ q = new MyBlockingQ();
    MyProducer p1 = new MyProducer(q);
    MyConsumer c1 = new MyConsumer(q);

    new Thread(p1).start();

    try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
    }        

    new Thread(c1).start();

    try {
        Thread.sleep(6000);
      } catch (InterruptedException e) {
        e.printStackTrace();
    }  
}