java - How to execute tasks in ExecutorService sequentially? -


i have 3 threads joined, i.e. second thread executes after first dies.

this code have:

public class main {     public static void main(string args[]) throws exception {         final thread thrda = new thread(() -> system.out.println("message 1"));         final thread thrdb = new thread(() -> system.out.println("message 2"));         final thread thrdc = new thread(() -> system.out.println("message 3"));          thrda.start();         thrda.join();         thrdb.start();         thrdb.join();         thrdc.start();         thrdc.join();      } } 

how implement functionality using executorservice instead of 3 thread objects?

if want/need execute group of jobs 1 after in single thread different main app thread, use executors#newsinglethreadexecutor.

executorservice es = executors.newsinglethreadexecutor(); es.submit(() -> system.out.println("message 1")); es.submit(() -> system.out.println("message 2")); es.submit(() -> system.out.println("message 3")); es.shutdown(); 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -