public class Cola { /* */
public static void main(String args[]) throws InterruptedException { PriorityQueue<Proceso> cola = new PriorityQueue<>();
for(int i = 0; i < 10; i++) { cola.add(new Proceso(String.format("Proc %d", i), i)); }
for(int i = 0; i < 150; i++) { Proceso p = cola.poll();
System.out.printf("Running %s\n", p.name); Thread.sleep(500);
p.runs++;
p.priority++;
cola.add(p);
}
}
/* */
static class Proceso implements Comparable<Proceso> { final String name;
private int priority;
private int runs;
/* */
Proceso(String name, int prio) { this.name = name;
this.priority = prio;
this.runs = 0;
}
@Override
public int compareTo(Proceso other) { if(other != null) { if(priority == other.priority) { return runs - other.runs;
}
return priority - other.priority;
}
return -1;
}
}
}