Reputation: 1388
I have created a thread (threadName = Original) in onCreate()
method of activity in android app. Now inside this thread (inside runnable), there is a for loop in which I created some new threads and then executed the threads in executorService like executorService.execute(thread)
, for each of them. After that, I created a timer in which I am checking that thread (Original) isAlive()
and when the Thread.isAlive()
is false then I call my recycler View.
Normally a thread does not die if we do not call executorService.shutdown()
. But in android, it dies without the executorService
being shutdown. Why this behaviour is seen in android studio?
Check with this example, our program will run indefinitely
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
}
});
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(thread1);
}
Upvotes: 0
Views: 193
Reputation: 1388
Here is the executor service
ExecutorService service = Executors.newCachedThreadPool();
service.shutdown()
- it means cannot execute anymore runnable, it does not mean that the executor is dead.
service.awaitTermination(20, TimeUnit.SECONDS)
- it means that the program will wait for 20 seconds and then it will die without finishing work. And also if the work is done before 20 seconds then everything is fine and it will die.
So in my case, after I used awaitTermination then it worked fine. My activity now waits for executor service to complete all the tasks.
Upvotes: 0
Reputation: 1048
The problem is that the 'original' thread finishes once it submits new tasks to the executor, unless you block it manually. This example shows how to wait until all tasks are finished or until a specified time-out occurs:
public static void main(String[] args) {
Runnable r = () -> {
ExecutorService service = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
Runnable run = () -> {
try {
Thread.sleep(15000);
System.out.println("Stopping Thread " + Thread.currentThread().getId());
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
};
service.submit(run);
}
try {
service.shutdown(); // shuts down once all submitted tasks complete
service.awaitTermination(18, TimeUnit.SECONDS); // blocks until all tasks complete after shutdown request or until time-out occurs
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
};
Thread original = new Thread(r);
original.start();
System.out.println("is Alive? " + original.isAlive());
while (original.isAlive()) {
// blocks until thread is not alive anymore
}
System.out.println("Original Thread Stopped");
}
I'd like to point out that it is very inefficient to use a for/while loop to constantly check whether a thread is still alive or not. Instead you could implement a listener that is invoked once all threads are finished.
Upvotes: 1