Android Open Source - android-sqlite-server Thread Affinity Executor






From Project

Back to project page android-sqlite-server.

License

The source code is released under:

Apache License

If you think the Android project android-sqlite-server listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.devtcg.sqliteserver.impl.binder;
//from  w w  w .  j  a  v a2  s  . co  m
import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

/**
 * This creates thread affinity with the client (all requests executing by this client on
 * the same thread will be matched onto a unique thread on the server side).  This is
 * important because {@link android.database.sqlite.SQLiteDatabase} stores state on a
 * per thread basis.
 */
public class ThreadAffinityExecutor<V> {
    private final Looper mLooper;
    private final Handler mHandler;

    public ThreadAffinityExecutor(Looper looper) {
        mLooper = looper;
        mHandler = new Handler(looper);
    }

    /**
     * Executes the Runnable on this executor's dedicated thread and wait for the result.
     *
     * @param callable Callable to run on the designated thread.
     * @return Callable result.
     * @throws ExecutionException Wrapped exception rethrown from the {@code callable}.
     */
    public V runSynchronously(Callable<V> callable) throws ExecutionException {
        if (Looper.myLooper() == mLooper) {
            return runOnCurrentThread(callable);
        } else {
            return runOnHandlerThread(callable);
        }
    }

    private V runOnCurrentThread(Callable<V> callable) throws ExecutionException {
        try {
            return callable.call();
        } catch (Exception e) {
            throw new ExecutionException(e);
        }
    }

    private V runOnHandlerThread(final Callable<V> callable) throws ExecutionException {
        // TODO: optimize this garbage (specifically: remove allocations).
        final ResultHolder<V> resultHolder = new ResultHolder<V>();
        final CountDownLatch resultLatch = new CountDownLatch(1);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    resultHolder.result = callable.call();
                } catch (Exception e) {
                    resultHolder.exception = e;
                } finally {
                    resultLatch.countDown();
                }
            }
        });
        awaitUninterruptibly(resultLatch);
        if (resultHolder.exception != null) {
            throw new ExecutionException(resultHolder.exception);
        } else {
            return resultHolder.result;
        }
    }

    private static void awaitUninterruptibly(CountDownLatch latch) {
        while (true) {
            try {
                latch.await();
                return;
            } catch (InterruptedException e) {
                // Restart loop...
            }
        }
    }

    public void shutdown() {
        // This is abrupt but in our execution model this should only be called when
        // the client either dies, or an orderly shutdown occurs.
        mLooper.quit();
    }

    private static class ResultHolder<Value> {
        public Value result;
        public Exception exception;
    }
}




Java Source Code List

aosp.android.database.BulkCursorDescriptor.java
aosp.android.database.BulkCursorNative.java
aosp.android.database.BulkCursorToCursorAdaptor.java
aosp.android.database.CrossProcessCursorWrapper.java
aosp.android.database.CursorToBulkCursorAdaptor.java
aosp.android.database.IBulkCursor.java
aosp.android.database.MoreDatabaseUtils.java
org.devtcg.sqliteserver.SQLiteContentProviderServer.java
org.devtcg.sqliteserver.SQLiteServerConnectionManager.java
org.devtcg.sqliteserver.SQLiteServerConnection.java
org.devtcg.sqliteserver.SQLiteServer.java
org.devtcg.sqliteserver.SQLiteServiceServer.java
org.devtcg.sqliteserver.exception.SQLiteServerException.java
org.devtcg.sqliteserver.impl.ExecutorHelper.java
org.devtcg.sqliteserver.impl.SQLiteExecutor.java
org.devtcg.sqliteserver.impl.ServerImplProvider.java
org.devtcg.sqliteserver.impl.binder.AbstractBinderClient.java
org.devtcg.sqliteserver.impl.binder.BinderHandle.java
org.devtcg.sqliteserver.impl.binder.BundleUtils.java
org.devtcg.sqliteserver.impl.binder.ClientTransactor.java
org.devtcg.sqliteserver.impl.binder.ContentObserverProxy.java
org.devtcg.sqliteserver.impl.binder.ContentProviderClient.java
org.devtcg.sqliteserver.impl.binder.SQLiteServerProtocolException.java
org.devtcg.sqliteserver.impl.binder.ServerImpl.java
org.devtcg.sqliteserver.impl.binder.ServerState.java
org.devtcg.sqliteserver.impl.binder.ServiceClient.java
org.devtcg.sqliteserver.impl.binder.ThreadAffinityExecutor.java
org.devtcg.sqliteserver.impl.binder.protocol.AbstractCommandHandler.java
org.devtcg.sqliteserver.impl.binder.protocol.AbstractCommandMessage.java
org.devtcg.sqliteserver.impl.binder.protocol.AcquireCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.BeginTransactionCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.DeleteCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.EndTransactionCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.ExceptionTransportHelper.java
org.devtcg.sqliteserver.impl.binder.protocol.ExecSQLCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.InsertCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.MethodName.java
org.devtcg.sqliteserver.impl.binder.protocol.RawQueryCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.ReleaseCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.SetTransactionSuccessfulCommand.java
org.devtcg.sqliteserver.impl.binder.protocol.UpdateCommand.java
org.devtcg.sqliteserver.sample.MyActivity.java
org.devtcg.sqliteserver.sample.MyOpenHelper.java
org.devtcg.sqliteserver.sample.TestContentProvider.java
org.devtcg.sqliteserver.sample.TestService.java