FileDocCategorySizeDatePackage
DelayedTask.javaAPI DocExample1719Mon Nov 13 15:18:32 GMT 2006collections

DelayedTask

public class DelayedTask extends Object implements Delayed

Fields Summary
public static final long
MILLISECONDS_IN_DAY
private long
endTime
private Task
task
Constructors Summary
DelayedTask(Task t, int daysDelay)

      
    endTime = System.currentTimeMillis() + daysDelay * MILLISECONDS_IN_DAY;
    task = t;
  
Methods Summary
public intcompareTo(java.util.concurrent.Delayed t)

    long thisDelay = getDelay(TimeUnit.MILLISECONDS);
    long otherDelay = t.getDelay(TimeUnit.MILLISECONDS);
    return (thisDelay < otherDelay) ? -1 : (thisDelay > otherDelay) ? 1 : 0;
  
public longgetDelay(java.util.concurrent.TimeUnit unit)

    long remainingTime = endTime - System.currentTimeMillis();
    return unit.convert(remainingTime, TimeUnit.MILLISECONDS);
  
TaskgetTask()

 return task; 
public static voidmain(java.lang.String[] args)

    BlockingQueue<DelayedTask> reminderQueue = new DelayQueue<DelayedTask>();
    reminderQueue.offer(new DelayedTask (databaseCode, 1));
    reminderQueue.offer(new DelayedTask (interfaceCode, 2));

    // now get the first reminder task that is ready to be processed
    DelayedTask t1 = reminderQueue.poll();
    if (t1 == null) {
      // no reminders ready yet
    } else {
      // process t1
    }
    Set<DelayedTask> delayedTaskSet1 = new HashSet<DelayedTask>();
    delayedTaskSet1.addAll(reminderQueue);
    Set<DelayedTask> delayedTaskSet2 = new HashSet<DelayedTask>();
    reminderQueue.drainTo(delayedTaskSet2);