0

I have the following test code for learning purposes, in which I am trying to run a producer and a consumer threads running endlessly on a blocking Q.

For some reason which I cannot understand, the output is as follows:

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

The code is as follows:

@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();
    }

I cannot understand why does the code stop executing after the last output line as shown above? I am working with JavaSE-15 on latest VSCode.

1 Answer 1

1

Threads are terminated, when your test method reaches to the end. If you want your threads to run longer, you need to add more sleep time at the end of your test method.

@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();
    }  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.