< prev index next >

src/java.net.http/share/classes/jdk/internal/net/http/TimeoutEvent.java

Print this page




  26 package jdk.internal.net.http;
  27 
  28 import java.time.Duration;
  29 import java.time.Instant;
  30 import java.util.concurrent.atomic.AtomicLong;
  31 
  32 /**
  33  * Timeout event notified by selector thread. Executes the given handler if
  34  * the timer not canceled first.
  35  *
  36  * Register with {@link HttpClientImpl#registerTimer(TimeoutEvent)}.
  37  *
  38  * Cancel with {@link HttpClientImpl#cancelTimer(TimeoutEvent)}.
  39  */
  40 abstract class TimeoutEvent implements Comparable<TimeoutEvent> {
  41 
  42     private static final AtomicLong COUNTER = new AtomicLong();
  43     // we use id in compareTo to make compareTo consistent with equals
  44     // see TimeoutEvent::compareTo below;
  45     private final long id = COUNTER.incrementAndGet();

  46     private final Instant deadline;
  47 
  48     TimeoutEvent(Duration duration) {

  49         deadline = Instant.now().plus(duration);
  50     }
  51 
  52     public abstract void handle();
  53 
  54     public Instant deadline() {
  55         return deadline;
  56     }
  57 
  58     @Override
  59     public int compareTo(TimeoutEvent other) {
  60         if (other == this) return 0;
  61         // if two events have the same deadline, but are not equals, then the
  62         // smaller is the one that was created before (has the smaller id).
  63         // This is arbitrary and we don't really care which is smaller or
  64         // greater, but we need a total order, so two events with the
  65         // same deadline cannot compare == 0 if they are not equals.
  66         final int compareDeadline = this.deadline.compareTo(other.deadline);
  67         if (compareDeadline == 0 && !this.equals(other)) {
  68             long diff = this.id - other.id; // should take care of wrap around
  69             if (diff < 0) return -1;
  70             else if (diff > 0) return 1;
  71             else assert false : "Different events with same id and deadline";
  72         }
  73         return compareDeadline;
  74     }
  75 
  76     @Override
  77     public String toString() {
  78         return "TimeoutEvent[id=" + id + ", deadline=" + deadline + "]";

  79     }
  80 }


  26 package jdk.internal.net.http;
  27 
  28 import java.time.Duration;
  29 import java.time.Instant;
  30 import java.util.concurrent.atomic.AtomicLong;
  31 
  32 /**
  33  * Timeout event notified by selector thread. Executes the given handler if
  34  * the timer not canceled first.
  35  *
  36  * Register with {@link HttpClientImpl#registerTimer(TimeoutEvent)}.
  37  *
  38  * Cancel with {@link HttpClientImpl#cancelTimer(TimeoutEvent)}.
  39  */
  40 abstract class TimeoutEvent implements Comparable<TimeoutEvent> {
  41 
  42     private static final AtomicLong COUNTER = new AtomicLong();
  43     // we use id in compareTo to make compareTo consistent with equals
  44     // see TimeoutEvent::compareTo below;
  45     private final long id = COUNTER.incrementAndGet();
  46     private final Duration duration;
  47     private final Instant deadline;
  48 
  49     TimeoutEvent(Duration duration) {
  50         this.duration = duration;
  51         deadline = Instant.now().plus(duration);
  52     }
  53 
  54     public abstract void handle();
  55 
  56     public Instant deadline() {
  57         return deadline;
  58     }
  59 
  60     @Override
  61     public int compareTo(TimeoutEvent other) {
  62         if (other == this) return 0;
  63         // if two events have the same deadline, but are not equals, then the
  64         // smaller is the one that was created before (has the smaller id).
  65         // This is arbitrary and we don't really care which is smaller or
  66         // greater, but we need a total order, so two events with the
  67         // same deadline cannot compare == 0 if they are not equals.
  68         final int compareDeadline = this.deadline.compareTo(other.deadline);
  69         if (compareDeadline == 0 && !this.equals(other)) {
  70             long diff = this.id - other.id; // should take care of wrap around
  71             if (diff < 0) return -1;
  72             else if (diff > 0) return 1;
  73             else assert false : "Different events with same id and deadline";
  74         }
  75         return compareDeadline;
  76     }
  77 
  78     @Override
  79     public String toString() {
  80         return "TimeoutEvent[id=" + id + ", duration=" + duration
  81                 + ", deadline=" + deadline + "]";
  82     }
  83 }
< prev index next >