public void testPriorityQueueDescending() { PriorityQueue pq = new PriorityQueue(false); try { pq.dequeue(); fail("NoSuchElementException not thrown on empty queue"); } catch (NoSuchElementException e) { } pq.enqueue("Test 2", 2); pq.enqueue("Test 1", 3); pq.enqueue("Test 3", 1); String peek1 = (String) pq.peek(); String s1 = (String) pq.dequeue(); String peek2 = (String) pq.peek(); String s2 = (String) pq.dequeue(); String s3 = (String) pq.dequeue(); try { pq.dequeue(); fail("NoSuchElementException not thrown on empty queue"); } catch (NoSuchElementException e) { } assertTrue("Peek #1 incorrect", peek1.equals("Test 1")); assertTrue("Peek #2 incorrect", peek2.equals("Test 2")); assertTrue("Dequeue #1 incorrect", s1.equals("Test 1")); assertTrue("Dequeue #2 incorrect", s2.equals("Test 2")); assertTrue("Dequeue #3 incorrect", s3.equals("Test 3")); }
@Test public void shouldNarrowQueue() { final PriorityQueue<Double> doubles = of(1.0d); final PriorityQueue<Number> numbers = PriorityQueue.narrow(doubles); final int actual = numbers.enqueue(new BigDecimal("2.0")).sum().intValue(); assertThat(actual).isEqualTo(3); }
/** * Adds a movie to the waiting queue. * * @pre movie must not be {@code null} * @post the movie will be added in the correct spot * @param m the movie to be added * @param priority the priority to add the movie at */ public void addMovietoWaiting(Movie m, int priority) { if (m == null) { return; } waitingQueue.enqueue(m, priority); }
public void MEC2(Process process) throws IOException { process.setState("Running"); int clock = 0; // while loop to execute instructions while (true) { clock++; if (isTermenated()) { // if the process terminates, then break the method process.setState("Terminated"); if (!IOBoundJob.isExist(process)) CPUBoundJob.enqueue(process, 0); Statistic.NUMBER_OF_EXECUTED_PROCESSES++; file.writeProcess(process); RAM.loadProcessFromHardDisk(); return; } if (isInterrupted(process)) { // if the process interrupted, then return the process to the Ready Queue (RAM) process.setState("Ready"); process.setRemainingTime(process.getRemainingTime() - clock); RAM.returnProcess(process); return; } } }
public void testPriorityQueueAscending() { PriorityQueue pq = new PriorityQueue(); try { pq.dequeue(); fail("NoSuchElementException not thrown on empty queue"); } catch (NoSuchElementException e) { } pq.enqueue("Test 2", 2); pq.enqueue("Test 3", 3); pq.enqueue("Test 1", 1); assertTrue("isEmpty is returned true on an nonempty queue", !pq.isEmpty()); String peek1 = (String) pq.peek(); String s1 = (String) pq.dequeue(); String peek2 = (String) pq.peek(); String s2 = (String) pq.dequeue(); String s3 = (String) pq.dequeue(); assertTrue("size was incorrect (should be 3)", pq.size() != 3); assertTrue("isEmpty is returned false on an empty queue", pq.isEmpty()); try { pq.dequeue(); fail("NoSuchElementException not thrown on empty queue"); } catch (NoSuchElementException e) { } assertTrue("Peek #1 incorrect", peek1.equals("Test 1")); assertTrue("Peek #2 incorrect", peek2.equals("Test 2")); assertTrue("Dequeue #1 incorrect", s1.equals("Test 1")); assertTrue("Dequeue #2 incorrect", s2.equals("Test 2")); assertTrue("Dequeue #3 incorrect", s3.equals("Test 3")); }
public static void main(String[] args) { PriorityQueue<Person> queue = new PriorityQueue<Person>(10); // PriorityQueue<Person> queue = new PriorityQueue<Person>(10,new // ReverseComparable<Person>()); // PriorityQueue<Person> queue = new PriorityQueue<Person>(10,new Wealth()); // PriorityQueue<Person> queue = new PriorityQueue<Person>(10,new Age()); Person[] list = { new Person("A", 20, 100), new Person("C", 28, 200), new Person("E", 24, 50), new Person("B", 28, 50), new Person("D", 28, 100), new Person("F", 24, 100) }; for (Person p : list) queue.enqueue(p); System.out.println("Size = " + queue.size()); System.out.println("Första element: " + queue.peek()); while (!queue.empty()) System.out.println("Element: " + queue.dequeue() + ", size = " + queue.size()); }
private boolean isInterrupted(Process process) { int num = ran.nextInt(99); if (num < 20) { int interrupt = ran.nextInt(99); if (interrupt >= 0 && interrupt < 20) { // I/O request IOBoundJob.enqueue(process, 0); } else if (interrupt >= 20 && interrupt < 40) { // the busy IO device will terminate } else return true; } return false; }
@Override public PriorityQueue<E> enqueue(E element) { if (isEmpty()) { head = element; return new LinkedList<>(element); } if (head.compareTo(element) == -1) { tail = tail.enqueue(element); } else { tail = new LinkedList<>(head, tail.copy()); head = element; } return new LinkedList<>(head, tail.copy()); }