Java LinkedBlockingDeque class

Description

Java LinkedBlockingDeque class

import java.util.Date;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

class Client implements Runnable {

  private LinkedBlockingDeque<String> requestList;

  public Client(LinkedBlockingDeque<String> requestList) {
    this.requestList = requestList;
  }//from  w ww . j  a v  a  2 s . co  m

  @Override
  public void run() {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 5; j++) {
        
        try {
          requestList.put(i + ":" + j);
          System.out.println(new Date());
          TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    System.out.printf("Client: End.\n");
  }
}

public class Main {
  public static void main(String[] args) throws Exception {
    LinkedBlockingDeque<String> list = new LinkedBlockingDeque<>(3);

    Client client = new Client(list);
    Thread thread = new Thread(client);
    thread.start();

    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 3; j++) {
        String request = list.take();
        System.out.println(request+" "+ new Date()+" "+ list.size());
      }
      TimeUnit.MILLISECONDS.sleep(300);
    }

    System.out.println("End of the program.");

  }

}



PreviousNext

Related