Browse Source

Use proper dates in QueueItems, fix broken logic.

Use Java8 date types for parser queue items. Fix the time check in
QueueHandler to check if the item has been queued for longer than
10 seconds, rather than if the item was created within 10 seconds
of the epoch.
pull/120/head
Chris Smith 8 years ago
parent
commit
1f0818efb2

+ 15
- 1
irc/src/com/dmdirc/parser/irc/outputqueue/QueueHandler.java View File

@@ -25,6 +25,8 @@ package com.dmdirc.parser.irc.outputqueue;
25 25
 import com.dmdirc.parser.common.QueuePriority;
26 26
 
27 27
 import java.io.PrintWriter;
28
+import java.time.LocalDateTime;
29
+import java.time.temporal.ChronoUnit;
28 30
 import java.util.Comparator;
29 31
 import java.util.concurrent.BlockingQueue;
30 32
 
@@ -99,7 +101,8 @@ public abstract class QueueHandler extends Thread implements Comparator<QueueIte
99 101
      */
100 102
     @Override
101 103
     public int compare(final QueueItem mainObject, final QueueItem otherObject) {
102
-        if (mainObject.getTime() < 10 * 1000 && mainObject.getPriority().compareTo(otherObject.getPriority()) != 0) {
104
+        if (!isStarved(mainObject)
105
+                && mainObject.getPriority().compareTo(otherObject.getPriority()) != 0) {
103 106
             return mainObject.getPriority().compareTo(otherObject.getPriority());
104 107
         }
105 108
 
@@ -113,6 +116,17 @@ public abstract class QueueHandler extends Thread implements Comparator<QueueIte
113 116
         }
114 117
     }
115 118
 
119
+    /**
120
+     * Determines whether the given item has been starved (i.e., it has sat waiting in the queue
121
+     * for too long due to higher priority items).
122
+     *
123
+     * @param item The item to check
124
+     * @return True if the item has been queued for longer than 10 seconds, false otherwise
125
+     */
126
+    private static boolean isStarved(final QueueItem item) {
127
+        return item.getTime().isBefore(LocalDateTime.now().minus(10, ChronoUnit.SECONDS));
128
+    }
129
+
116 130
     /**
117 131
      * This is the main even loop of the queue.
118 132
      * It needs to handle pulling items out of the queue and calling

+ 7
- 5
irc/src/com/dmdirc/parser/irc/outputqueue/QueueItem.java View File

@@ -24,6 +24,8 @@ package com.dmdirc.parser.irc.outputqueue;
24 24
 
25 25
 import com.dmdirc.parser.common.QueuePriority;
26 26
 
27
+import java.time.LocalDateTime;
28
+
27 29
 /**
28 30
  * Queued Item.
29 31
  */
@@ -34,7 +36,7 @@ public class QueueItem implements Comparable<QueueItem> {
34 36
     /** Line to send. */
35 37
     private final String line;
36 38
     /** Time this line was added. */
37
-    private final long time;
39
+    private final LocalDateTime time;
38 40
     /** Item Number. */
39 41
     private final long itemNumber;
40 42
     /** What is the priority of this line? */
@@ -54,7 +56,7 @@ public class QueueItem implements Comparable<QueueItem> {
54 56
         this.line = line;
55 57
         this.priority = priority;
56 58
 
57
-        this.time = System.currentTimeMillis();
59
+        this.time = LocalDateTime.now();
58 60
         this.itemNumber = number++;
59 61
     }
60 62
 
@@ -68,11 +70,11 @@ public class QueueItem implements Comparable<QueueItem> {
68 70
     }
69 71
 
70 72
     /**
71
-     * Get the value of time.
73
+     * Gets the time at which this item was queued.
72 74
      *
73
-     * @return the value of time
75
+     * @return the time this item was queued.
74 76
      */
75
-    public long getTime() {
77
+    public LocalDateTime getTime() {
76 78
         return time;
77 79
     }
78 80
 

Loading…
Cancel
Save