Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util.thread;
 
 
 
 /* ------------------------------------------------------------ */
Timeout queue. This class implements a timeout queue for timers that are at least as likely to be cancelled as they are to expire. Unlike the util timeout class, the duration of the timeouts is shared by all scheduled tasks and if the duration is changed, this affects all scheduled tasks.

The nested class Task should be extended by users of this class to obtain call back notification of expires.

 
 public class Timeout
 {
     private static final Logger LOG = Log.getLogger(Timeout.class);
     private Object _lock;
     private long _duration;
     private volatile long _now=System.currentTimeMillis();
     private Task _head=new Task();
 
     /* ------------------------------------------------------------ */
     public Timeout()
     {
         =new Object();
         .=this;
     }
 
     /* ------------------------------------------------------------ */
     public Timeout(Object lock)
     {
         =lock;
         .=this;
     }
 
     /* ------------------------------------------------------------ */
    

Returns:
Returns the duration.
 
     public long getDuration()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
    

Parameters:
duration The duration to set.
 
     public void setDuration(long duration)
     {
          = duration;
     }
 
     /* ------------------------------------------------------------ */
     public long setNow()
     {
         return =System.currentTimeMillis();
     }
     
     /* ------------------------------------------------------------ */
     public long getNow()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
     public void setNow(long now)
     {
         =now;
     }
 
     /* ------------------------------------------------------------ */
    
Get an expired tasks. This is called instead of tick() to obtain the next expired Task, but without calling it's Timeout.Task.expire() or Timeout.Task.expired() methods.

Returns:
the next expired task or null.
 
    public Task expired()
    {
        synchronized ()
        {
            long _expiry = -;
            if (.!=)
            {
                Task task = .;
                if (task._timestamp>_expiry)
                    return null;
                task.unlink();
                task._expired=true;
                return task;
            }
            return null;
        }
    }
    /* ------------------------------------------------------------ */
    public void tick()
    {
        final long expiry = -;
        Task task=null;
        while (true)
        {
            try
            {
                synchronized ()
                {
                    task.;
                    if (task== || task._timestamp>expiry)
                        break;
                    task.unlink();
                    task._expired=true;
                    task.expire();
                }
                
                task.expired();
            }
            catch(Throwable th)
            {
                .warn(.,th);
            }
        }
    }
    /* ------------------------------------------------------------ */
    public void tick(long now)
    {
        =now;
        tick();
    }
    /* ------------------------------------------------------------ */
    public void schedule(Task task)
    {
        schedule(task,0L);
    }
    
    /* ------------------------------------------------------------ */
    

Parameters:
task
delay A delay in addition to the default duration of the timeout
    public void schedule(Task task,long delay)
    {
        synchronized ()
        {
            if (task._timestamp!=0)
            {
                task.unlink();
                task._timestamp=0;
            }
            task._timeout=this;
            task._expired=false;
            task._delay=delay;
            task._timestamp = +delay;
            Task last=.;
            while (last!=)
            {
                if (last._timestamp <= task._timestamp)
                    break;
                last=last._prev;
            }
            last.link(task);
        }
    }
    /* ------------------------------------------------------------ */
    public void cancelAll()
    {
        synchronized ()
        {
            .=.=;
        }
    }
    /* ------------------------------------------------------------ */
    public boolean isEmpty()
    {
        synchronized ()
        {
            return .==;
        }
    }
    /* ------------------------------------------------------------ */
    public long getTimeToNext()
    {
        synchronized ()
        {
            if (.==)
                return -1;
            long to_next = +..-;
            return to_next<0?0:to_next;
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public String toString()
    {
        StringBuffer buf = new StringBuffer();
        buf.append(super.toString());
        
        Task task = .;
        while (task!=)
        {
            buf.append("-->");
            buf.append(task);
            task=task._next;
        }
        
        return buf.toString();
    }
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    
Task. The base class for scheduled timeouts. This class should be extended to implement the expire() method, which is called if the timeout expires.
    public static class Task
    {
        Task _next;
        Task _prev;
        Timeout _timeout;
        long _delay;
        long _timestamp=0;
        boolean _expired=false;
        /* ------------------------------------------------------------ */
        protected Task()
        {
            ==this;
        }
        /* ------------------------------------------------------------ */
        public long getTimestamp()
        {
            return ;
        }
        /* ------------------------------------------------------------ */
        public long getAge()
        {
            final Timeout t = ;
            if (t!=null)
            {
                final long now=t._now;
                if (now!=0 && !=0)
                    return now-;
            }
            return 0;
        }
        /* ------------------------------------------------------------ */
        private void unlink()
        {
            .=;
            .=;
            ==this;
            =false;
        }
        /* ------------------------------------------------------------ */
        private void link(Task task)
        {
            Task next_next = ;
            .=task;
            =task;
            .=next_next;
            .=this;   
        }
        
        /* ------------------------------------------------------------ */
        
Schedule the task on the given timeout. The task exiry will be called after the timeout duration.

Parameters:
timer
        public void schedule(Timeout timer)
        {
            timer.schedule(this);
        }
        
        /* ------------------------------------------------------------ */
        
Schedule the task on the given timeout. The task exiry will be called after the timeout duration.

Parameters:
timer
        public void schedule(Timeout timerlong delay)
        {
            timer.schedule(this,delay);
        }
        
        /* ------------------------------------------------------------ */
        
Reschedule the task on the current timeout. The task timeout is rescheduled as if it had been cancelled and scheduled on the current timeout.
        public void reschedule()
        {
            Timeout timeout = ;
            if (timeout!=null)
                timeout.schedule(this,);
        }
        
        /* ------------------------------------------------------------ */
        
Cancel the task. Remove the task from the timeout.
        public void cancel()
        {
            Timeout timeout = ;
            if (timeout!=null)
            {
                synchronized (timeout._lock)
                {
                    unlink();
                    =0;
                }
            }
        }
        
        /* ------------------------------------------------------------ */
        public boolean isExpired() { return ; }
        /* ------------------------------------------------------------ */
	public boolean isScheduled() { return !=this; }
        
        /* ------------------------------------------------------------ */
        
Expire task. This method is called when the timeout expires. It is called in the scope of the synchronize block (on this) that sets the isExpired() state to true.

See also:
expired() For an unsynchronized callback.
        protected void expire(){}
        /* ------------------------------------------------------------ */
        
Expire task. This method is called when the timeout expires. It is called outside of any synchronization scope and may be delayed.
        public void expired(){}
    }
New to GrepCode? Check out our FAQ X