/**
  * Constrains the values of all delays in the queue to be within Long.MAX_VALUE of each other, to
  * avoid overflow in compareTo. This may occur if a task is eligible to be dequeued, but has not
  * yet been, while some other task is added with a delay of Long.MAX_VALUE.
  */
 private long overflowFree(long delay) {
   Delayed head = (Delayed) super.getQueue().peek();
   if (head != null) {
     long headDelay = head.getDelay(NANOSECONDS);
     if (headDelay < 0 && (delay - headDelay < 0)) delay = Long.MAX_VALUE + headDelay;
   }
   return delay;
 }
Esempio n. 2
0
 /**
  * Run a IO action as main program. *
  *
  * <p>Called from the java <tt>main</tt> method of a frege program. This converts the argument
  * String array to a list and passes this to the compiled frege main function. The result is an IO
  * action of type <tt>IO ()</tt> to which then <tt>IO.performUnsafe</tt> is applied. The resulting
  * {@link Lambda} then actually executes the frege code when evaluated.
  */
 public static void runMain(final Object arg) {
   try {
     Delayed.delayed(arg).call();
   }
   //		catch (Exception ex) {
   //			throw new Error(ex); // ex.printStackTrace();
   //		}
   finally {
     stderr.flush();
     stdout.flush();
   }
   return;
 }
 public int compareTo(Delayed other) {
   if (other == this) // compare zero if same object
   return 0;
   if (other instanceof ScheduledFutureTask) {
     ScheduledFutureTask<?> x = (ScheduledFutureTask<?>) other;
     long diff = time - x.time;
     if (diff < 0) return -1;
     else if (diff > 0) return 1;
     else if (sequenceNumber < x.sequenceNumber) return -1;
     else return 1;
   }
   long diff = (getDelay(NANOSECONDS) - other.getDelay(NANOSECONDS));
   return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
 }