nie używałem ale na stronie projektu jest dość jasno wszystko opisane
AQuery
ewentualnie zamiast budować własny stos skorzystaj ze zmodyfikowanego AsyncTaks tak żeby wykonywane było tylko jedno zadanie na raz, wtedy będziesz mogła alokować większe obszary pamięci, dodać statyny bufor, piękne to może nie jest ale działa
public abstract class Threded<Params, Progress, Result> {
private static final String LOG_TAG = "Threded";
private static final ThreadPoolExecutor mExecutor = new ThreadPoolExecutor(1, 20, 20, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
private static final int MESSAGE_POST_RESULT = 0x1;
private static final int MESSAGE_POST_PROGRESS = 0x2;
private static final int MESSAGE_POST_CANCEL = 0x3;
private static final InternalHandler sHandler = new InternalHandler();
private final WorkerRunnable<Params, Result> mWorker;
private final FutureTask<Result> mFuture;
private volatile Status mStatus = Status.PENDING;
public enum Status {PENDING, RUNNING, FINISHED}
private static ByteArrayBuffer mBuffer;
public Threded(int size) {
if (mBuffer == null) {
mBuffer = new ByteArrayBuffer(size);
}
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return doInBackground(mBuffer, mParams);
}
};
mFuture = new FutureTask<Result>(mWorker) {
@Override
@SuppressWarnings("unchecked")
protected void done() {
Message message;
Result result = null;
try {
result = get();
} catch (InterruptedException e) {
android.util.Log.w(LOG_TAG, e);
} catch (ExecutionException e) {
throw new RuntimeException("An error occured while executing doInBackground()", e.getCause());
} catch (CancellationException e) {
message = sHandler.obtainMessage(MESSAGE_POST_CANCEL, new AsyncTaskResult<Result>(Threded.this, (Result[]) null));
message.sendToTarget();
return;
} catch (Throwable t) {
throw new RuntimeException("An error occured while executing " + "doInBackground()", t);
}
message = sHandler.obtainMessage(MESSAGE_POST_RESULT, new AsyncTaskResult<Result>(Threded.this, result));
message.sendToTarget();
}
};
}
public final Status getStatus() {
return mStatus;
}
protected abstract Result doInBackground(ByteArrayBuffer buffer, Params... params);
protected void onPreExecute() {}
protected void onPostExecute(Result result) {}
protected void onProgressUpdate(Progress... values) {}
protected void onCancelled() {}
public final boolean isCancelled() {
return mFuture.isCancelled();
}
public final boolean cancel(boolean mayInterruptIfRunning) {
return mFuture.cancel(mayInterruptIfRunning);
}
public final Result get() throws InterruptedException, ExecutionException {
return mFuture.get();
}
public final Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return mFuture.get(timeout, unit);
}
public final Threded<Params, Progress, Result> execute(Params... params) {
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
throw new IllegalStateException("Cannot execute task:" + " the task is already running.");
case FINISHED:
throw new IllegalStateException("Cannot execute task:" + " the task has already been executed " + "(a task can be executed only once)");
}
}
mStatus = Status.RUNNING;
onPreExecute();
mWorker.mParams = params;
mExecutor.execute(mFuture);
return this;
}
protected final void publishProgress(Progress... values) {
sHandler.obtainMessage(MESSAGE_POST_PROGRESS, new AsyncTaskResult<Progress>(this, values)).sendToTarget();
}
private void finish(Result result) {
if (isCancelled()) result = null;
onPostExecute(result);
mStatus = Status.FINISHED;
}
private static class InternalHandler extends Handler {
@Override
@SuppressWarnings("unchecked")
public void handleMessage(Message msg) {
AsyncTaskResult result = (AsyncTaskResult) msg.obj;
switch (msg.what) {
case MESSAGE_POST_RESULT:
result.mTask.finish(result.mData[0]);
break;
case MESSAGE_POST_PROGRESS:
result.mTask.onProgressUpdate(result.mData);
break;
case MESSAGE_POST_CANCEL:
result.mTask.onCancelled();
break;
}
}
}
private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
Params[] mParams;
}
private static class AsyncTaskResult<Data> {
final Threded mTask;
final Data[] mData;
AsyncTaskResult(Threded task, Data... data) {
mTask = task;
mData = data;
}
}
}
metoda doInBackground dostarcza jeszcze jeden argument, bufor nie musisz się martwić bufor jest w każdej instancji klasy ten sam pamiętaj jedynie żeby po zapisie nie odczytywać całości ale tyle ile zostało zapisane w konkretnym cyklu