How to execute tasks in ExecutorService sequentially?

后端 未结 3 741
遇见更好的自我
遇见更好的自我 2020-12-10 14:38

I have three threads that are joined, i.e. the second thread executes after the first dies.

This is the code I have:

public class Main {
    public s         


        
3条回答
  •  猫巷女王i
    2020-12-10 15:37

    If what you want/need is to execute a group of jobs one after another but in a single thread different that the main app thread, then 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();
    

提交回复
热议问题