while we want to execute 2 Runnables :
executor.execute(new Runnable1());
executor.execute(new Runnable2());
it is unknown that which Runnable will start to be executed first? but I want Runnable1 be started for execution first. how to do that?
Why not create one runnable which just runs
Runnable1()thenRunnable2()? If you don’t want them to execute in parallel, don’t submit them both to an executor separately…For example:
Of course, you should consider what you want to happen if
Runnable1throws an unchecked exception – do you wantRunnable2to run or not?EDIT: With your updated requirements, it sounds like really you want your second runnable to only start when your first runnable has reached a particular point (e.g. a socket has been created and is listening for connections). You quite possibly want to put some sort of hook in that code, so that you can start the second runnable when you get there: