示例#1
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();
 }
示例#3
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();
  }
示例#5
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;
     }
 }
 public static void main(String[] args) throws InterruptedException {
   final int NUM = 10;
   Producer producer = new Producer(10);
   Consumer[] consumers = new Consumer[NUM];
   for (int i = 0; i < NUM; i++) {
     consumers[i] = new Consumer(producer);
   }
   System.out.println("\t== START ==");
   producer.start();
   for (int i = 0; i < NUM; i++) {
     consumers[i].join();
   }
   System.out.println("\t== OVER ==");
 }
  /** @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;
      }
    }
  }
  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();
  }