示例#1
0
  public static void main(String[] args) throws Exception {
    ExecutorService exec = Executors.newCachedThreadPool();
    for (int i = 0; i < 5; i++) exec.execute(new Task());
    exec.execute(new Task2());
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(
        new TimerTask() {
          boolean prod = true;

          public void run() {
            if (prod) {
              System.out.print("\nnotify() ");
              Task.blocker.prod();
              prod = false;
            } else {
              System.out.print("\nnotifyAll() ");
              Task.blocker.prodAll();
              prod = true;
            }
          }
        },
        400,
        400); // Run every .4 second
    TimeUnit.SECONDS.sleep(5); // Run for a while...
    timer.cancel();
    System.out.println("\nTimer canceled");
    TimeUnit.MILLISECONDS.sleep(500);
    System.out.print("Task2.blocker.prodAll() ");
    Task2.blocker.prodAll();
    TimeUnit.MILLISECONDS.sleep(500);
    System.out.println("\nShutting down");
    exec.shutdownNow(); // Interrupt all tasks
  }
示例#2
0
 // Insert a random delay to produce the effect
 // of a calculation time:
 private void pause(int factor) {
   try {
     TimeUnit.MILLISECONDS.sleep(100 + rand.nextInt(factor));
   } catch (InterruptedException e) {
     print("sleep() interrupted");
   }
 }
示例#3
0
 @Override
 public boolean setIfEmpty(T data, long time, TimeUnit units) {
   long end = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(time, units);
   while (!setIfEmpty(data)) {
     if (end < System.currentTimeMillis()) return false;
   }
   return true;
 }
 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");
 }
 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");
 }
 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()) {
       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");
 }
 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");
 }
 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");
 }
示例#10
0
 /**
  * @return true if this memtable is expired. Expiration time is determined by CF's
  *     memtable_flush_period_in_ms.
  */
 public boolean isExpired() {
   int period = cfs.metadata.params.memtableFlushPeriodInMs;
   return period > 0
       && (System.nanoTime() - creationNano >= TimeUnit.MILLISECONDS.toNanos(period));
 }
示例#11
0
 private void pause() throws InterruptedException {
   if (ponderFactor == 0) return;
   TimeUnit.MILLISECONDS.sleep(rand.nextInt(ponderFactor * 250));
 }