@Test public void testOffer(){ JobPriorityBlockingDeque deque = new JobPriorityBlockingDeque(300); for (int i = 0; i < 20; i++) { JobPo jobPo = new JobPo(); jobPo.setJobId("21312" + (i%3)); jobPo.setPriority(i); jobPo.setGmtModified(Long.valueOf(20-i)); deque.offer(jobPo); } int size = deque.size(); for (int i = 0; i < size; i++) { System.out.println(i + " : " + deque.pollLast()); } }
public boolean offer(JobPo e) { if (e == null) throw new NullPointerException(); if (list.size() >= capacity) return false; lock.lock(); try { if (jobs.containsKey(e.getJobId())) { Long gmtModified = jobs.get(e.getJobId()); if (gmtModified != null && !gmtModified.equals(e.getGmtModified())) { removeOld(e); } } int insertionPoint = Collections.binarySearch(list, e, comparator); if (insertionPoint < 0) { insertionPoint = -insertionPoint - 1; } list.add(insertionPoint, e); jobs.put(e.getJobId(), e.getGmtModified()); return true; } finally { lock.unlock(); } }
JobPriorityBlockingDeque { public boolean offer(JobPo e) { if (e == null) throw new NullPointerException(); if (list.size() >= capacity) return false; lock.lock(); try { if (jobs.containsKey(e.getJobId())) { Long gmtModified = jobs.get(e.getJobId()); if (gmtModified != null && !gmtModified.equals(e.getGmtModified())) { removeOld(e); } } int insertionPoint = Collections.binarySearch(list, e, comparator); if (insertionPoint < 0) { insertionPoint = -insertionPoint - 1; } list.add(insertionPoint, e); jobs.put(e.getJobId(), e.getGmtModified()); return true; } finally { lock.unlock(); } } }
JobPriorityBlockingDeque { public boolean offer(JobPo e) { if (e == null) throw new NullPointerException(); if (list.size() >= capacity) return false; lock.lock(); try { if (jobs.containsKey(e.getJobId())) { Long gmtModified = jobs.get(e.getJobId()); if (gmtModified != null && !gmtModified.equals(e.getGmtModified())) { removeOld(e); } } int insertionPoint = Collections.binarySearch(list, e, comparator); if (insertionPoint < 0) { insertionPoint = -insertionPoint - 1; } list.add(insertionPoint, e); jobs.put(e.getJobId(), e.getGmtModified()); return true; } finally { lock.unlock(); } } JobPriorityBlockingDeque(int capacity); }
JobPriorityBlockingDeque { public boolean offer(JobPo e) { if (e == null) throw new NullPointerException(); if (list.size() >= capacity) return false; lock.lock(); try { if (jobs.containsKey(e.getJobId())) { Long gmtModified = jobs.get(e.getJobId()); if (gmtModified != null && !gmtModified.equals(e.getGmtModified())) { removeOld(e); } } int insertionPoint = Collections.binarySearch(list, e, comparator); if (insertionPoint < 0) { insertionPoint = -insertionPoint - 1; } list.add(insertionPoint, e); jobs.put(e.getJobId(), e.getGmtModified()); return true; } finally { lock.unlock(); } } JobPriorityBlockingDeque(int capacity); JobPo pollFirst(); JobPo pollLast(); boolean offer(JobPo e); JobPo poll(); int size(); @Override String toString(); Iterator<JobPo> iterator(); }
JobPriorityBlockingDeque { public boolean offer(JobPo e) { if (e == null) throw new NullPointerException(); if (list.size() >= capacity) return false; lock.lock(); try { if (jobs.containsKey(e.getJobId())) { Long gmtModified = jobs.get(e.getJobId()); if (gmtModified != null && !gmtModified.equals(e.getGmtModified())) { removeOld(e); } } int insertionPoint = Collections.binarySearch(list, e, comparator); if (insertionPoint < 0) { insertionPoint = -insertionPoint - 1; } list.add(insertionPoint, e); jobs.put(e.getJobId(), e.getGmtModified()); return true; } finally { lock.unlock(); } } JobPriorityBlockingDeque(int capacity); JobPo pollFirst(); JobPo pollLast(); boolean offer(JobPo e); JobPo poll(); int size(); @Override String toString(); Iterator<JobPo> iterator();  }