Question

I have a large Android project with 50 distinct code instances of the newSingleThreadExecutor() that are triggered by various unpredictable events including UI button presses. After researching a bit, I think that a single thread is created for each of the 50 distinct code instances and will maintain sequential order of execution of calls to the executor.

How many threads are created and is there a problem in maintaining sequential execution in order of calls to the executor?

Note: the code is not using submit so this is why I am confused about the order of execution and number of threads created. For example, what happens if a user presses the UI button an hundred times very fast? Will I get a hundred threads finishing at different times based on when the database futures complete?

Listed below is how the code is formatted in all 50 instances.

Executors.newSingleThreadExecutor().execute(new Runnable() {
    @Override
        public void run()
        { 
            // Executes multiple database calls using futures and
            // Uses get() which waits if necessary for the computation to complete, 
            // and then retrieves its result.
        }
    });
Was it helpful?

Solution

How many threads are created and is there a problem in maintaining sequential execution in order of calls to the executor?

I'm not sure I'm understanding whether or not you are asking about the pool threads or the threads forked each time through the loop.

So if you are creating a new ExecutorService each of the 50 times then you cannot tell how many threads are actually created. If all of the 49 previous jobs are still running then the 50th will create another thread. If each of the jobs finish before the next one is executed then only one thread will be created. This doesn't take into account the number of futures and other stuff down by each thread. If it is already a background thread do you really need multiple futures, etc.?

Without more details, it seems to be me that the right thing to do here is to use a single ExecutorService for all 50 jobs. Then you can determine if you want a fixed number of threads or make it dynamic.

I think that a single thread is created for each of the 50 distinct code instances and will maintain sequential order of execution of calls to the executor.

There will be no "sequential order" because you are creating a new executor service each time. If you were using a single one then the only way to guarantee sequential order of execution is to only have one thread in the pool.

For example, what happens if a user presses the UI button an hundred times very fast? Will I get a hundred threads finishing at different times based on when the database futures complete?

Yes, you will get a 100 different threads. If you want to control the max number of threads forked then again, use a single thread-pool for all of the tasks.

Btw, you must always shutdown an executor service after you submit the last job:

ExecutorService threadPool = Executors.newSingleThreadExecutor();
threadPool.execute(new Runnable() { ... });
// this must be done to properly quit the threads
threadPool.shutdown();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top