Code example for ScheduledExecutorService

Methods: shutdownsubmit

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. 
Experience pair programming with AI  Get Codota for Java