FileDocCategorySizeDatePackage
DispatchThread.javaAPI DocAndroid 5.1 API4953Thu Mar 12 22:22:48 GMT 2015com.android.ex.camera2.portability

DispatchThread

public class DispatchThread extends Thread

Fields Summary
private static final Log.Tag
TAG
private static final long
MAX_MESSAGE_QUEUE_LENGTH
private final Queue
mJobQueue
private Boolean
mIsEnded
private android.os.Handler
mCameraHandler
private android.os.HandlerThread
mCameraHandlerThread
Constructors Summary
public DispatchThread(android.os.Handler cameraHandler, android.os.HandlerThread cameraHandlerThread)


         
        super("Camera Job Dispatch Thread");
        mJobQueue = new LinkedList<Runnable>();
        mIsEnded = new Boolean(false);
        mCameraHandler = cameraHandler;
        mCameraHandlerThread = cameraHandlerThread;
    
Methods Summary
public voidend()
Gracefully ends this thread. Will stop after all jobs are processed.

        synchronized (mIsEnded) {
            mIsEnded = true;
        }
        synchronized(mJobQueue) {
            mJobQueue.notifyAll();
        }
    
private booleanisEnded()

        synchronized (mIsEnded) {
            return mIsEnded;
        }
    
public voidrun()

        while(true) {
            Runnable job = null;
            synchronized (mJobQueue) {
                while (mJobQueue.size() == 0 && !isEnded()) {
                    try {
                        mJobQueue.wait();
                    } catch (InterruptedException ex) {
                        Log.w(TAG, "Dispatcher thread wait() interrupted, exiting");
                        break;
                    }
                }

                job = mJobQueue.poll();
            }

            if (job == null) {
                // mJobQueue.poll() returning null means wait() is
                // interrupted and the queue is empty.
                if (isEnded()) {
                    break;
                }
                continue;
            }

            job.run();

            synchronized (DispatchThread.this) {
                mCameraHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (DispatchThread.this) {
                            DispatchThread.this.notifyAll();
                        }
                    }
                });
                try {
                    DispatchThread.this.wait();
                } catch (InterruptedException ex) {
                    // TODO: do something here.
                }
            }
        }
        mCameraHandlerThread.quitSafely();
    
public voidrunJob(java.lang.Runnable job)
Queues up the job.

param
job The job to run.

        if (isEnded()) {
            throw new IllegalStateException(
                    "Trying to run job on interrupted dispatcher thread");
        }
        synchronized (mJobQueue) {
            if (mJobQueue.size() == MAX_MESSAGE_QUEUE_LENGTH) {
                throw new RuntimeException("Camera master thread job queue full");
            }

            mJobQueue.add(job);
            mJobQueue.notifyAll();
        }
    
public voidrunJobSync(java.lang.Runnable job, java.lang.Object waitLock, long timeoutMs, java.lang.String jobMsg)
Queues up the job and wait for it to be done.

param
job The job to run.
param
timeoutMs Timeout limit in milliseconds.
param
jobMsg The message to log when the job runs timeout.
return
Whether the job finishes before timeout.

        String timeoutMsg = "Timeout waiting " + timeoutMs + "ms for " + jobMsg;
        synchronized (waitLock) {
            long timeoutBound = SystemClock.uptimeMillis() + timeoutMs;
            try {
                runJob(job);
                waitLock.wait(timeoutMs);
                if (SystemClock.uptimeMillis() > timeoutBound) {
                    throw new IllegalStateException(timeoutMsg);
                }
            } catch (InterruptedException ex) {
                if (SystemClock.uptimeMillis() > timeoutBound) {
                    throw new IllegalStateException(timeoutMsg);
                }
            }
        }