コード例 #1
0
ファイル: SynchronizedQueue.java プロジェクト: kykong/POSA-14
        public void run() {
          for (int i = 0; i < mMaxIterations; i++)
            try {
              if (Thread.interrupted()) {
                throw new InterruptedException();
              }
              Integer result = (Integer) mQueue.take();

              System.out.println("iteration = " + result);
            } catch (InterruptedException e) {
              System.out.println(
                  "Thread properly interrupted by " + e.toString() + " in consumerRunnable");
              // This isn't an error - it just means that
              // we've been interrupted by the main Thread.
              return;
            } catch (TimeoutException e) {
              System.out.println("Exception " + e.toString() + " occurred in consumerRunnable");
              // Indicate a timeout.
              mConsumerCounter = TIMEOUT_OCCURRED;
              return;
            } catch (Exception e) {
              System.out.println("Exception " + e.toString() + " occurred in consumerRunnable");
              // Indicate a failure.
              mConsumerCounter = FAILURE_OCCURRED;
              return;
            }
        }
コード例 #2
0
ファイル: DelayQueueDemo.java プロジェクト: jackode/most-java
 public void run() {
   try {
     while (!Thread.interrupted()) q.take().run(); // Run task with the current thread
   } catch (InterruptedException e) {
     // Acceptable way to exit
   }
   print("Finished DelayedTaskConsumer");
 }
コード例 #3
0
 synchronized void waitingCall() {
   try {
     while (!Thread.interrupted()) {
       wait();
       System.out.print(Thread.currentThread() + " ");
     }
   } catch (InterruptedException e) {
     // OK to exit this way
   }
 }
コード例 #4
0
ファイル: CarBuilder.java プロジェクト: quietcoolwu/TIJ4
 public void run() {
   try {
     while (!Thread.interrupted()) {
       print(carQueue.take());
     }
   } catch (InterruptedException e) {
     print("Exiting Reporter via interrupt");
   }
   print("Reporter off");
 }
コード例 #5
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       for (int i = 0; i < ExchangerDemo.size; i++) holder.add(generator.next());
       // Exchange full for empty:
       holder = exchanger.exchange(holder);
     }
   } catch (InterruptedException e) {
     // OK to terminate this way.
   }
 }
 public void run() {
   try {
     while (!Thread.interrupted()) {
       TimeUnit.MILLISECONDS.sleep(rand.nextInt(300));
       customers.put(new Customer(rand.nextInt(1000)));
     }
   } catch (InterruptedException e) {
     System.out.println("CustomerGenerator interrupted");
   }
   System.out.println("CustomerGenerator terminating");
 }
コード例 #7
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       Event event = q.take();
       print(event);
       event.run();
     }
   } catch (InterruptedException e) {
     // Acceptable way to exit
   }
   print("Finished Controller");
 }
コード例 #8
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       // Blocks until a course is ready
       Plate plate = filledOrders.take();
       print(this + "received " + plate + " delivering to " + plate.getOrder().getCustomer());
       plate.getOrder().getCustomer().deliver(plate);
     }
   } catch (InterruptedException e) {
     print(this + " interrupted");
   }
   print(this + " off duty");
 }
コード例 #9
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       car.waitForWaxing();
       printnb("Wax Off! ");
       TimeUnit.MILLISECONDS.sleep(200);
       car.buffed();
     }
   } catch (InterruptedException e) {
     print("Exiting via interrupt");
   }
   print("Ending Wax Off task");
 }
コード例 #10
0
  /**
   * Waits for all workers to finish.
   *
   * @param cancel Flag to indicate whether workers should be cancelled before waiting for them to
   *     finish.
   */
  public void join(boolean cancel) {
    if (cancel) U.cancel(workers);

    // Record current interrupted status of calling thread.
    boolean interrupted = Thread.interrupted();

    try {
      U.join(workers, log);
    } finally {
      // Reset interrupted flag on calling thread.
      if (interrupted) Thread.currentThread().interrupt();
    }
  }
コード例 #11
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       // Blocks until next piece of toast is available:
       Toast t = dryQueue.take();
       t.butter();
       print(t);
       butteredQueue.put(t);
     }
   } catch (InterruptedException e) {
     print("Butterer interrupted");
   }
   print("Butterer off");
 }
コード例 #12
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       holder = exchanger.exchange(holder);
       for (T x : holder) {
         value = x; // Fetch out value
         holder.remove(x); // OK for CopyOnWriteArrayList
       }
     }
   } catch (InterruptedException e) {
     // OK to terminate this way.
   }
   System.out.println("Final value: " + value);
 }
コード例 #13
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       // A new customer arrives; assign a WaitPerson:
       WaitPerson wp = waitPersons.get(rand.nextInt(waitPersons.size()));
       Customer c = new Customer(wp);
       exec.execute(c);
       TimeUnit.MILLISECONDS.sleep(100);
     }
   } catch (InterruptedException e) {
     print("Restaurant interrupted");
   }
   print("Restaurant closing");
 }
 public void run() {
   try {
     while (!Thread.interrupted()) {
       TimeUnit.MILLISECONDS.sleep(adjustmentPeriod);
       adjustTellerNumber();
       System.out.print(customers + " { ");
       for (Teller teller : workingTellers) System.out.print(teller.shortString() + " ");
       System.out.println("}");
     }
   } catch (InterruptedException e) {
     System.out.println(this + "interrupted");
   }
   System.out.println(this + "terminating");
 }
コード例 #15
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       TimeUnit.MILLISECONDS.sleep(100 + rand.nextInt(500));
       // Make toast
       Toast t = new Toast(count++);
       print(t);
       // Insert into queue
       toastQueue.put(t);
     }
   } catch (InterruptedException e) {
     print("Toaster interrupted");
   }
   print("Toaster off");
 }
コード例 #16
0
ファイル: CarBuilder.java プロジェクト: quietcoolwu/TIJ4
 public void run() {
   try {
     while (!Thread.interrupted()) {
       TimeUnit.MILLISECONDS.sleep(500);
       // Make chassis:
       Car c = new Car(counter++);
       print("ChassisBuilder created " + c);
       // Insert into queue
       carQueue.put(c);
     }
   } catch (InterruptedException e) {
     print("Interrupted: ChassisBuilder");
   }
   print("ChassisBuilder off");
 }
コード例 #17
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       synchronized (this) {
         strides += rand.nextInt(3); // Produces 0, 1 or 2
       }
       barrier.await();
     }
   } catch (InterruptedException e) {
     // A legitimate way to exit
   } catch (BrokenBarrierException e) {
     // This one we want to know about
     throw new RuntimeException(e);
   }
 }
 public void run() {
   try {
     while (!Thread.interrupted()) {
       Customer customer = customers.take();
       TimeUnit.MILLISECONDS.sleep(customer.getServiceTime());
       synchronized (this) {
         customersServed++;
         while (!servingCustomerLine) wait();
       }
     }
   } catch (InterruptedException e) {
     System.out.println(this + "interrupted");
   }
   System.out.println(this + "terminating");
 }
コード例 #19
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       // Blocks until an order appears:
       Order order = restaurant.orders.take();
       Food requestedItem = order.item();
       // Time to prepare order:
       TimeUnit.MILLISECONDS.sleep(rand.nextInt(500));
       Plate plate = new Plate(order, requestedItem);
       order.getWaitPerson().filledOrders.put(plate);
     }
   } catch (InterruptedException e) {
     print(this + " interrupted");
   }
   print(this + " off duty");
 }
コード例 #20
0
 public void run() {
   done.countDown();
   remaining.incrementAndGet();
   int n;
   while (!Thread.interrupted() && (n = remaining.get()) > 0 && done.getCount() > 0) {
     if (remaining.compareAndSet(n, n - 1)) {
       try {
         pool.execute(this);
       } catch (RuntimeException ex) {
         System.out.print("*");
         while (done.getCount() > 0) done.countDown();
         return;
       }
     }
   }
 }
コード例 #21
0
ファイル: CarBuilder.java プロジェクト: quietcoolwu/TIJ4
 public void run() {
   try {
     powerDown(); // Wait until needed
     while (!Thread.interrupted()) {
       performService();
       assembler.barrier().await(); // Synchronize
       // We're done with that job...
       powerDown();
     }
   } catch (InterruptedException e) {
     print("Exiting " + this + " via interrupt");
   } catch (BrokenBarrierException e) {
     // This one we want to know about
     throw new RuntimeException(e);
   }
   print(this + " off");
 }
コード例 #22
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       // Blocks until next piece of toast is available:
       Toast t = finishedQueue.take();
       // Verify that the toast is coming in order,
       // and that all pieces are getting jammed:
       if (t.getId() != counter++ || t.getStatus() != Toast.Status.JAMMED) {
         print(">>>> Error: " + t);
         System.exit(1);
       } else print("Chomp! " + t);
     }
   } catch (InterruptedException e) {
     print("Eater interrupted");
   }
   print("Eater off");
 }
コード例 #23
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       print(this + " " + "thinking");
       pause();
       // Philosopher becomes hungry
       print(this + " " + "grabbing right");
       right.take();
       print(this + " " + "grabbing left");
       left.take();
       print(this + " " + "eating");
       pause();
       right.drop();
       left.drop();
     }
   } catch (InterruptedException e) {
     print(this + " " + "exiting via interrupt");
   }
 }
コード例 #24
0
ファイル: CarBuilder.java プロジェクト: quietcoolwu/TIJ4
 public void run() {
   try {
     while (!Thread.interrupted()) {
       // Blocks until chassis is available:
       car = chassisQueue.take();
       // Hire robots to perform work:
       robotPool.hire(EngineRobot.class, this);
       robotPool.hire(DriveTrainRobot.class, this);
       robotPool.hire(WheelRobot.class, this);
       barrier.await(); // Until the robots finish
       // Put car into finishingQueue for further work
       finishingQueue.put(car);
     }
   } catch (InterruptedException e) {
     print("Exiting Assembler via interrupt");
   } catch (BrokenBarrierException e) {
     // This one we want to know about
     throw new RuntimeException(e);
   }
   print("Assembler off");
 }
コード例 #25
0
    public void run() {
      while (!Thread.interrupted()) {
        // Randomly select an element to work on:
        int element = rand.nextInt(N_ELEMENTS);
        for (int i = 0; i < N_GENES; i++) {
          int previous = element - 1;
          if (previous < 0) previous = N_ELEMENTS - 1;
          int next = element + 1;
          if (next >= N_ELEMENTS) next = 0;
          int oldvalue = GRID[element][i].get();

          // Perform some kind of modeling calculation:
          int newvalue = oldvalue + GRID[previous][i].get() + GRID[next][i].get();
          newvalue /= 3; // Average the three values
          if (!GRID[element][i].compareAndSet(oldvalue, newvalue)) {
            // Policy here to deal with failure. Here, we
            // just report it and ignore it; our model
            // will eventually deal with it.
            System.out.println("Old value changed from " + oldvalue);
          }
        }
      }
    }
コード例 #26
0
ファイル: BaseTest.java プロジェクト: fanlu/MyApp
 public void foo() throws InterruptedException {
   if (Thread.interrupted()) {
     throw new InterruptedException();
   }
 }