Code example for ScheduledExecutorService

Methods: awaitTermination, shutdown, submit

0
     * See class comment for details about {@code JsonWriter} construction. 
     * 
     * @param onClosed run after the close() operation has completed asynchronously 
     */ 
    private synchronized void close(final Runnable onClosed) {
        mExecutor.submit(new Callable<Object>() {
            @Override 
            public Object call() throws Exception {
                try { 
                    if (mJsonWriter == null) return null;
                    // TODO: This is necessary to avoid an exception.  Better would be to not even 
                    // open the JsonWriter if the file is not even opened unless there is valid data 
                    // to write. 
                    if (!mHasWrittenData) {
                        mJsonWriter.beginArray();
                    } 
                    mJsonWriter.endArray();
                    mHasWrittenData = false;
                    mJsonWriter.flush();
                    mJsonWriter.close();
                    if (DEBUG) {
                        Log.d(TAG, "closed " + mFile);
                    } 
                } catch (final Exception e) {
                    Log.d(TAG, "error when closing ResearchLog:", e);
                } finally { 
                    // Marking the file as read-only signals that this log file is ready to be 
                    // uploaded. 
                    if (mFile != null && mFile.exists()) {
                        mFile.setWritable(false, false);
                    } 
                    if (onClosed != null) {
                        onClosed.run();
                    } 
                } 
                return null; 
            } 
        }); 
        removeAnyScheduledFlush(); 
        mExecutor.shutdown();
    } 
 
    /** 
     * Block until the research log has shut down and spooled out all output or {@code timeout} 
     * occurs. 
     * 
     * @param timeout time to wait for close in milliseconds 
     */ 
    public void blockingClose(final long timeout) {
        close(null); 
        awaitTermination(timeout, TimeUnit.MILLISECONDS);
    } 
 
    /** 
     * Waits for publication requests to finish, closes the JsonWriter, but then deletes the backing 
     * output file. 
     * 
     * @param onAbort run after the abort() operation has completed asynchronously 
     */ 
    private synchronized void abort(final Runnable onAbort) {
        mExecutor.submit(new Callable<Object>() {
            @Override 
            public Object call() throws Exception {
                try { 
                    if (mJsonWriter == null) return null;
                    if (mHasWrittenData) {
                        // TODO: This is necessary to avoid an exception.  Better would be to not 
                        // even open the JsonWriter if the file is not even opened unless there is 
                        // valid data to write. 
                        if (!mHasWrittenData) {
                            mJsonWriter.beginArray();
                        } 
                        mJsonWriter.endArray();
                        mJsonWriter.close();
                        mHasWrittenData = false;
                    } 
                } finally { 
                    if (mFile != null) {
                        mFile.delete();
                    } 
                    if (onAbort != null) {
                        onAbort.run();
                    } 
                } 
                return null; 
            } 
        }); 
        removeAnyScheduledFlush(); 
        mExecutor.shutdown();
    } 
 
    /** 
     * Block until the research log has aborted or {@code timeout} occurs. 
     * 
     * @param timeout time to wait for close in milliseconds 
     */ 
    public void blockingAbort(final long timeout) {
        abort(null); 
        awaitTermination(timeout, TimeUnit.MILLISECONDS);
    } 
 
    @UsedForTesting 
    public void awaitTermination(final long delay, final TimeUnit timeUnit) {
        try { 
            if (!mExecutor.awaitTermination(delay, timeUnit)) {
                Log.e(TAG, "ResearchLog executor timed out while awaiting terminaion");
            } 
        } catch (final InterruptedException e) {
            Log.e(TAG, "ResearchLog executor interrupted while awaiting terminaion", e);
        }