Execute operations for same priority ordered

pull/172/head
M66B 6 years ago
parent 97f99a583f
commit c7f7bfb3cd

@ -179,7 +179,9 @@ public class Helper {
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
RunnableFuture<T> task = super.newTaskFor(runnable, value); RunnableFuture<T> task = super.newTaskFor(runnable, value);
if (runnable instanceof PriorityRunnable) if (runnable instanceof PriorityRunnable)
return new PriorityFuture<T>(task, ((PriorityRunnable) runnable).getPriority()); return new PriorityFuture<T>(task,
((PriorityRunnable) runnable).getPriority(),
((PriorityRunnable) runnable).getOrder());
else else
return task; return task;
} }
@ -221,17 +223,22 @@ public class Helper {
private static class PriorityFuture<T> implements RunnableFuture<T> { private static class PriorityFuture<T> implements RunnableFuture<T> {
private int priority; private int priority;
private long order;
private RunnableFuture<T> wrapped; private RunnableFuture<T> wrapped;
PriorityFuture(RunnableFuture<T> wrapped, int priority) { PriorityFuture(RunnableFuture<T> wrapped, int priority, long order) {
this.priority = priority;
this.wrapped = wrapped; this.wrapped = wrapped;
this.priority = priority;
this.order = order;
} }
public int getPriority() { public int getPriority() {
return priority; return this.priority;
} }
public long getOrder() {
return this.order;
}
@Override @Override
public void run() { public void run() {
@ -270,8 +277,13 @@ public class Helper {
if (r1 instanceof PriorityFuture<?> && r2 instanceof PriorityFuture<?>) { if (r1 instanceof PriorityFuture<?> && r2 instanceof PriorityFuture<?>) {
Integer p1 = ((PriorityFuture<?>) r1).getPriority(); Integer p1 = ((PriorityFuture<?>) r1).getPriority();
Integer p2 = ((PriorityFuture<?>) r2).getPriority(); Integer p2 = ((PriorityFuture<?>) r2).getPriority();
Log.i("Priority " + p1 + "/" + p2 + "=" + p1.compareTo(p2)); int p = p1.compareTo(p2);
return p1.compareTo(p2); if (p == 0) {
Long o1 = ((PriorityFuture<?>) r1).getOrder();
Long o2 = ((PriorityFuture<?>) r2).getOrder();
return o1.compareTo(o2);
} else
return p;
} else } else
return 0; return 0;
} }
@ -279,13 +291,19 @@ public class Helper {
static class PriorityRunnable implements Runnable { static class PriorityRunnable implements Runnable {
private int priority; private int priority;
private long order;
int getPriority() { int getPriority() {
return priority; return this.priority;
}
long getOrder() {
return this.order;
} }
PriorityRunnable(int priority) { PriorityRunnable(int priority, long order) {
this.priority = priority; this.priority = priority;
this.order = order;
} }
@Override @Override

@ -1161,9 +1161,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Integer p2 = k2.getPriority(); Integer p2 = k2.getPriority();
int priority = p1.compareTo(p2); int priority = p1.compareTo(p2);
if (priority == 0) { if (priority == 0) {
Long t1 = k1.getTime(); Long o1 = k1.getOrder();
Long t2 = k2.getTime(); Long o2 = k2.getOrder();
return t1.compareTo(t2); return o1.compareTo(o2);
} else } else
return priority; return priority;
} }
@ -1177,7 +1177,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
try { try {
executor.submit(new Helper.PriorityRunnable(key.getPriority()) { executor.submit(new Helper.PriorityRunnable(key.getPriority(), key.getOrder()) {
@Override @Override
public void run() { public void run() {
super.run(); super.run();

@ -48,7 +48,7 @@ public class TupleOperationEx extends EntityOperation {
PartitionKey getPartitionKey(boolean offline) { PartitionKey getPartitionKey(boolean offline) {
PartitionKey key = new PartitionKey(); PartitionKey key = new PartitionKey();
key.time = this.created; key.order = this.id;
if (offline) { if (offline) {
// open/close folder is expensive // open/close folder is expensive
@ -75,13 +75,13 @@ public class TupleOperationEx extends EntityOperation {
} }
class PartitionKey { class PartitionKey {
public long time; private long order;
private int priority; private int priority;
private String id; private String id;
private String operation; private String operation;
long getTime() { long getOrder() {
return time; return this.order;
} }
int getPriority() { int getPriority() {

Loading…
Cancel
Save