I pass the thread to the Executorservice in the android

final ExecutorService downloadService= Executors.newSingleThreadExecutor(); Thread t = new Thread() { @Override public void run() { DriveContents contents = result.getDriveContents(); try { InputStream is = contents.getInputStream(); OutputStream os = new FileOutputStream(file1); byte[] buffer = new byte[1024]; int bytesRead; //read from is to buffer while((bytesRead = is.read(buffer)) !=-1){ os.write(buffer, 0, bytesRead); } is.close(); //flush OutputStream to write any buffered data to file os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } downloadService.submit(t); 

but he gives an error

 java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@298a067f rejected from java.util.concurrent.ThreadPoolExecutor@2f9a64c[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0] 

    1 answer 1

    RejectedExecutionException is issued in two cases:

    1. Pooled queue full
    2. A shutdown() was called

    Therefore, if you post a short code snooping on your code, check that a) you are not submitting any more tasks, b) whether shutdown() called somewhere before the submit() call.

    Another option is that shutdown() is called by the GC. Executors.newSingleThreadExecutor() returns an instance of FinalizableDelegatedExecutorService , which is a wrapper over a real thread pool and when finalized, shutdown() called. Therefore, there is a chance that the GC gets to the "unused" instance before submit() called.

    • yes yes shutdown was called, before completion. - J Mas