replaced Thread initialization with ExecutorService pool for delete

method. This is much faster and produces less blocking when using the
Compressor class which is used by the HTCache. I.e. picture search is
much faster now.
pull/1/head
Michael Peter Christen 13 years ago
parent a58dc4a91f
commit 37f2d1b3e9

@ -41,7 +41,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
@ -561,7 +563,7 @@ public class ArrayStack implements BLOB {
if (bi.blob.containsKey(key)) return bi; if (bi.blob.containsKey(key)) return bi;
return null; return null;
} }
// first check the current blob only because that has most probably the key if any has that key // first check the current blob only because that has most probably the key if any has that key
int bs1 = this.blobs.size() - 1; int bs1 = this.blobs.size() - 1;
blobItem bi = this.blobs.get(bs1); blobItem bi = this.blobs.get(bs1);
@ -572,7 +574,7 @@ public class ArrayStack implements BLOB {
if (bi.blob.containsKey(key)) return bi; if (bi.blob.containsKey(key)) return bi;
return null; return null;
} }
// start a concurrent query to database tables // start a concurrent query to database tables
final CompletionService<blobItem> cs = new ExecutorCompletionService<blobItem>(this.executor); final CompletionService<blobItem> cs = new ExecutorCompletionService<blobItem>(this.executor);
int accepted = 0; int accepted = 0;
@ -839,30 +841,35 @@ public class ArrayStack implements BLOB {
final blobItem bi = this.blobs.get(0); final blobItem bi = this.blobs.get(0);
bi.blob.delete(key); bi.blob.delete(key);
} else { } else {
final Thread[] t = new Thread[this.blobs.size() - 1]; @SuppressWarnings("unchecked")
final FutureTask<Boolean>[] t = new FutureTask[this.blobs.size() - 1];
int i = 0; int i = 0;
for (final blobItem bi: this.blobs) { for (final blobItem bi: this.blobs) {
if (i < t.length) { if (i < t.length) {
// run this in a concurrent thread // run this in a concurrent thread
final blobItem bi0 = bi; final blobItem bi0 = bi;
t[i] = new Thread() { t[i] = new FutureTask<Boolean>(new Callable<Boolean>() {
@Override @Override
public void run() { public Boolean call() {
try { bi0.blob.delete(key); } catch (final IOException e) {} try { bi0.blob.delete(key); } catch (final IOException e) {}
return true;
} }
}; });
t[i].start(); DELETE_EXECUTOR.execute(t[i]);
} else { } else {
// no additional thread, run in this thread // no additional thread, run in this thread
try { bi.blob.delete(key); } catch (final IOException e) {} try { bi.blob.delete(key); } catch (final IOException e) {}
} }
i++; i++;
} }
for (final Thread s: t) try {s.join();} catch (final InterruptedException e) {} // wait for termination
for (final FutureTask<Boolean> s: t) try {s.get();} catch (final InterruptedException e) {} catch (ExecutionException e) {}
} }
assert mem() <= m : "m = " + m + ", mem() = " + mem(); assert mem() <= m : "m = " + m + ", mem() = " + mem();
} }
private static final ExecutorService DELETE_EXECUTOR = Executors.newFixedThreadPool(128);
/** /**
* close the BLOB * close the BLOB
*/ */

Loading…
Cancel
Save