예제 #1
0
  @Test
  public void testTwoThreads() throws InterruptedException {

    final BlockingQueue<Integer> queue = new LocklessBlockingQueue<>(1 << 4);

    // final BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1 <<
    // 4);

    Consumer c1 = new Consumer(queue);
    Consumer c2 = new Consumer(queue);
    c1.start();
    c2.start();

    for (int i = 0; i < 100; i++) {
      queue.put(i);
    }

    long currentTimeMillis = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
      queue.put(i);
    }
    long timr = System.currentTimeMillis() - currentTimeMillis;
    Thread.sleep(5000);
    System.out.println(timr);
  }
예제 #2
0
 // 여러 쓰레드에서 하나의 object를 공유할 때 한 쓰레드가 해당 object를 사용하면 다른 쓰레드들은 사용이 종료될 때까지 기다려야 하는 경우
 // wait()/notify()를 사용하면 된다.
 private void exampleWaitAndNotify() {
   Queue queue = new Queue();
   Producer p = new Producer(queue);
   Consumer c = new Consumer(queue);
   p.start();
   c.start();
 }
 public static void main(String[] args) {
   CubbyHole c = new CubbyHole();
   Producer p1 = new Producer(c, 1);
   Consumer c1 = new Consumer(c, 1);
   p1.start();
   c1.start();
 }
예제 #4
0
 public static void main(String[] args) {
   Properties properties = new Properties();
   properties.put(PropertyKeyConst.ConsumerId, "CID_SHOWCAL_SMS");
   properties.put(PropertyKeyConst.AccessKey, "tNCKjDgxH84GDbRl");
   properties.put(PropertyKeyConst.SecretKey, "Mg1QihOlUEsuQtVtFZyAZtGXZEXjk8");
   Consumer consumer = ONSFactory.createConsumer(properties);
   consumer.subscribe(
       "SHOWCAL_SMS",
       "*",
       new MessageListener() {
         public Action consume(Message message, ConsumeContext context) {
           String messagestr = null;
           try {
             messagestr = new String(message.getBody(), "UTF-8");
           } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
           }
           System.out.println("消息 --->" + messagestr);
           SMSMessage smsMessage = JSON.parseObject(messagestr, SMSMessage.class);
           SMSTool.getInstance().SendMsg(smsMessage.getMessage(), smsMessage.getMobilePhone());
           return Action.CommitMessage;
         }
       });
   consumer.start();
   System.out.println("SHOWCAL SMS Start");
 }
  public static void main(String[] args) {
    // Producer producerThread = new Producer(KafkaProperties.topic);
    // producerThread.start();

    Consumer consumerThread = new Consumer(KafkaProperties.topic);
    consumerThread.start();
  }
예제 #6
0
  // main for testing consumer/producer
  public static void main(String[] args) {
    // 한개의 버퍼를 생성후 이를 공유하는 두개의 스레드 : 소비자, 생성자를 동작
    IntBuffer ib = new IntBuffer();

    Consumer c = new Consumer(ib);
    Producer p = new Producer(ib);

    c.start();
    p.start();
  }
  public static void main(String[] args) throws InterruptedException {

    CubbyHole c = new CubbyHole();
    Producer p1 = new Producer(c);
    Consumer c1 = new Consumer(c);

    p1.start();
    c1.start();
    p1.join();
    c1.join();
  }
예제 #8
0
 // 主程式
 public static void main(String[] args) {
   // 建立執行緒物件
   Producer producer = new Producer();
   Consumer consumer = new Consumer();
   // 啟動執行緒
   producer.start();
   consumer.start();
   while (true) // 結束條件
   if (producer.count >= MAXITEMS && producer.count == consumer.count) {
       isRunning = false;
       break;
     }
 }
  /** @param args */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Producer product = new Producer();
    Consumer consume = new Consumer();
    ProducerConsumerQueue queue = new ProducerConsumerQueue();
    boolean loops = true;
    while (loops) {
      System.out.println("please choose your option:\n1.Set menu\n2.Order\n3.Exit");
      Scanner input = new Scanner(System.in);
      int choiceOfPizza = input.nextInt();

      switch (choiceOfPizza) {
        case 1:
          product.start();
          queue.put(product);
          loops = false;
          try {
            product.join();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          System.out.println("continue?(y/n)");
          if (input.next().equalsIgnoreCase("y")) {
            loops = true;
          }
          break;
        case 2:
          consume.start();
          queue.get(consume);
          loops = false;
          try {
            consume.join();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          System.out.println("continue?(y/n)");
          if (input.next().equalsIgnoreCase("y")) {
            loops = true;
          }
          break;
        default:
          System.out.println("Thank you, see you again");
          System.exit(1);
          break;
      }
    }
  }
예제 #10
0
  public static void main(String[] args) throws InterruptedException {
    s1.put(4);
    s1.put(5);
    s1.put(8);
    s1.put(12);
    s1.put(21);
    s1.put(22);
    s1.put(34);
    s1.put(35);
    s1.put(36);
    s1.put(37);
    s1.put(42);

    Producer t1 = new Producer();
    Producer t2 = new Producer();
    Producer t3 = new Producer();
    Producer t4 = new Producer();
    Consumer c1 = new Consumer();
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    c1.start();
  }
 @Override
 public void initialize() {
   consumer.start();
 }