Thread Newbie: Joining thread one after another with concurrent












1














I would like to run thread one after another.
Is there any alternative way to Marathon with Java 8?
Without using ExecuterService:



public class Marathon {

public static void main(String args) throws InterruptedException {

Runnable task = () -> {
for (int i = 0; i < 10; i++) {

System.out.println(Thread.currentThread().getName()+ " is running... " + i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
};

Thread t1 = new Thread(task, "Mary");
Thread t2 = new Thread(task, "David");
t1.start();
t1.join(100);
t2.start();
}
}


Output:



Mary is running... 0 
David is running... 0
Mary is running... 1
David is running... 1
...


Following code doesn't work as Marathon :



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
}
TimeUnit.MILLISECONDS.sleep(100);

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
job1.get(500, TimeUnit.MILLISECONDS);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}


Output:



pool-1-thread-1 is running... 0



...



pool-1-thread-1 is running... 9



pool-1-thread-2 is running... 0



...



pool-1-thread-2 is running... 9



Is it possible to do with ExecuterService?



Expected:



pool-1-thread-1 is running... 0
pool-1-thread-2 is running... 0
...
pool-1-thread-1 is running... 9
pool-1-thread-2 is running... 9









share|improve this question




















  • 1




    task.run(); t2.start();? The whole point of threads is to execute concurrently. If you want sequential execution, why use threads in the first place?
    – JB Nizet
    Dec 28 '18 at 9:40










  • Yeah as @JBNizet said if you want them to run synchronously then why use threads? But I think you want to read the results synchronously and in order but run them async.
    – Sam Orozco
    Dec 28 '18 at 9:43
















1














I would like to run thread one after another.
Is there any alternative way to Marathon with Java 8?
Without using ExecuterService:



public class Marathon {

public static void main(String args) throws InterruptedException {

Runnable task = () -> {
for (int i = 0; i < 10; i++) {

System.out.println(Thread.currentThread().getName()+ " is running... " + i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
};

Thread t1 = new Thread(task, "Mary");
Thread t2 = new Thread(task, "David");
t1.start();
t1.join(100);
t2.start();
}
}


Output:



Mary is running... 0 
David is running... 0
Mary is running... 1
David is running... 1
...


Following code doesn't work as Marathon :



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
}
TimeUnit.MILLISECONDS.sleep(100);

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
job1.get(500, TimeUnit.MILLISECONDS);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}


Output:



pool-1-thread-1 is running... 0



...



pool-1-thread-1 is running... 9



pool-1-thread-2 is running... 0



...



pool-1-thread-2 is running... 9



Is it possible to do with ExecuterService?



Expected:



pool-1-thread-1 is running... 0
pool-1-thread-2 is running... 0
...
pool-1-thread-1 is running... 9
pool-1-thread-2 is running... 9









share|improve this question




















  • 1




    task.run(); t2.start();? The whole point of threads is to execute concurrently. If you want sequential execution, why use threads in the first place?
    – JB Nizet
    Dec 28 '18 at 9:40










  • Yeah as @JBNizet said if you want them to run synchronously then why use threads? But I think you want to read the results synchronously and in order but run them async.
    – Sam Orozco
    Dec 28 '18 at 9:43














1












1








1


0





I would like to run thread one after another.
Is there any alternative way to Marathon with Java 8?
Without using ExecuterService:



public class Marathon {

public static void main(String args) throws InterruptedException {

Runnable task = () -> {
for (int i = 0; i < 10; i++) {

System.out.println(Thread.currentThread().getName()+ " is running... " + i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
};

Thread t1 = new Thread(task, "Mary");
Thread t2 = new Thread(task, "David");
t1.start();
t1.join(100);
t2.start();
}
}


Output:



Mary is running... 0 
David is running... 0
Mary is running... 1
David is running... 1
...


Following code doesn't work as Marathon :



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
}
TimeUnit.MILLISECONDS.sleep(100);

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
job1.get(500, TimeUnit.MILLISECONDS);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}


Output:



pool-1-thread-1 is running... 0



...



pool-1-thread-1 is running... 9



pool-1-thread-2 is running... 0



...



pool-1-thread-2 is running... 9



Is it possible to do with ExecuterService?



Expected:



pool-1-thread-1 is running... 0
pool-1-thread-2 is running... 0
...
pool-1-thread-1 is running... 9
pool-1-thread-2 is running... 9









share|improve this question















I would like to run thread one after another.
Is there any alternative way to Marathon with Java 8?
Without using ExecuterService:



public class Marathon {

public static void main(String args) throws InterruptedException {

Runnable task = () -> {
for (int i = 0; i < 10; i++) {

System.out.println(Thread.currentThread().getName()+ " is running... " + i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
};

Thread t1 = new Thread(task, "Mary");
Thread t2 = new Thread(task, "David");
t1.start();
t1.join(100);
t2.start();
}
}


Output:



Mary is running... 0 
David is running... 0
Mary is running... 1
David is running... 1
...


Following code doesn't work as Marathon :



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
}
TimeUnit.MILLISECONDS.sleep(100);

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
job1.get(500, TimeUnit.MILLISECONDS);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}


Output:



pool-1-thread-1 is running... 0



...



pool-1-thread-1 is running... 9



pool-1-thread-2 is running... 0



...



pool-1-thread-2 is running... 9



Is it possible to do with ExecuterService?



Expected:



pool-1-thread-1 is running... 0
pool-1-thread-2 is running... 0
...
pool-1-thread-1 is running... 9
pool-1-thread-2 is running... 9






java multithreading executorservice






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 10:28







Alice

















asked Dec 28 '18 at 9:30









AliceAlice

7051821




7051821








  • 1




    task.run(); t2.start();? The whole point of threads is to execute concurrently. If you want sequential execution, why use threads in the first place?
    – JB Nizet
    Dec 28 '18 at 9:40










  • Yeah as @JBNizet said if you want them to run synchronously then why use threads? But I think you want to read the results synchronously and in order but run them async.
    – Sam Orozco
    Dec 28 '18 at 9:43














  • 1




    task.run(); t2.start();? The whole point of threads is to execute concurrently. If you want sequential execution, why use threads in the first place?
    – JB Nizet
    Dec 28 '18 at 9:40










  • Yeah as @JBNizet said if you want them to run synchronously then why use threads? But I think you want to read the results synchronously and in order but run them async.
    – Sam Orozco
    Dec 28 '18 at 9:43








1




1




task.run(); t2.start();? The whole point of threads is to execute concurrently. If you want sequential execution, why use threads in the first place?
– JB Nizet
Dec 28 '18 at 9:40




task.run(); t2.start();? The whole point of threads is to execute concurrently. If you want sequential execution, why use threads in the first place?
– JB Nizet
Dec 28 '18 at 9:40












Yeah as @JBNizet said if you want them to run synchronously then why use threads? But I think you want to read the results synchronously and in order but run them async.
– Sam Orozco
Dec 28 '18 at 9:43




Yeah as @JBNizet said if you want them to run synchronously then why use threads? But I think you want to read the results synchronously and in order but run them async.
– Sam Orozco
Dec 28 '18 at 9:43












2 Answers
2






active

oldest

votes


















0














The two classes are not doing the same thing. You can probably reach the solution yourself by comparing them closely. First, do you know exactly how your first class (Marathon) works? In particular, what do you think the following line does?



t1.join(100);


The thread t1, which has just started running, has just gone into a loop which counts up once every 200 milliseconds. The join(100) call simply causes the current (main) thread to wait 100 milliseconds. You will achieve exactly the same results by replacing that line with this one:



Thread.sleep(100);



Now that the main thread has slept for 100 milliseconds, it starts thread t2. Now the two threads are running in parallel, and every 200 milliseconds both threads output a line, the second thread delayed by 100 milliseconds so that they appear evenly interleaved.



Now let's look at your second method, Marathon2. A few differences from the first class are immediately obvious:




  1. The sleep in the Runnable is outside the loop, instead of inside.

  2. The sleep in the Runnable is only 100 milliseconds, instead of 200.

  3. The maximum wait in the main thread is 500 milliseconds, instead of 100.

  4. The Future.get method causes a TimeoutException instead of just continuing. We can simply replace this call with a sleep anyway, since that's all that the first class does.


So, ironing out the differences, we get the following Marathon2 class which behaves in a similar manner to the other class (Marathon), with interleaved threads:



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
TimeUnit.MILLISECONDS.sleep(200);
}

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
TimeUnit.MILLISECONDS.sleep(100);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}





share|improve this answer





















  • Thanks you. I thought Future.get(long timeout, TimeUnit unit) is same as Thread.join(long millis). I could also use service.awaitTermination(100, TimeUnit.MILLISECONDS) instead of Thread.sleep(100);
    – Alice
    Dec 29 '18 at 9:50



















1














Without dealing with any threads nor with Executors directly you can do it with a CompletableFuture



 Runnable runnable = () -> System.out.println("hi");
Runnable runnable1 = () -> System.out.println("there");
CompletableFuture<Void> all = CompletableFuture.runAsync(runnable).thenRun(runnable1);

all.whenComplete((x,th) -> {
System.out.println("both done");
});


Note that this would use the common ForkJoin pool but you can still provide your own.






share|improve this answer























  • Not quite the interleaved output the OP was looking for.
    – DodgyCodeException
    Dec 28 '18 at 12:58










  • I just found out ExecutorService.invokeAll(callable) also output sequential order.
    – Alice
    Dec 29 '18 at 9:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53956312%2fthread-newbie-joining-thread-one-after-another-with-concurrent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The two classes are not doing the same thing. You can probably reach the solution yourself by comparing them closely. First, do you know exactly how your first class (Marathon) works? In particular, what do you think the following line does?



t1.join(100);


The thread t1, which has just started running, has just gone into a loop which counts up once every 200 milliseconds. The join(100) call simply causes the current (main) thread to wait 100 milliseconds. You will achieve exactly the same results by replacing that line with this one:



Thread.sleep(100);



Now that the main thread has slept for 100 milliseconds, it starts thread t2. Now the two threads are running in parallel, and every 200 milliseconds both threads output a line, the second thread delayed by 100 milliseconds so that they appear evenly interleaved.



Now let's look at your second method, Marathon2. A few differences from the first class are immediately obvious:




  1. The sleep in the Runnable is outside the loop, instead of inside.

  2. The sleep in the Runnable is only 100 milliseconds, instead of 200.

  3. The maximum wait in the main thread is 500 milliseconds, instead of 100.

  4. The Future.get method causes a TimeoutException instead of just continuing. We can simply replace this call with a sleep anyway, since that's all that the first class does.


So, ironing out the differences, we get the following Marathon2 class which behaves in a similar manner to the other class (Marathon), with interleaved threads:



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
TimeUnit.MILLISECONDS.sleep(200);
}

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
TimeUnit.MILLISECONDS.sleep(100);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}





share|improve this answer





















  • Thanks you. I thought Future.get(long timeout, TimeUnit unit) is same as Thread.join(long millis). I could also use service.awaitTermination(100, TimeUnit.MILLISECONDS) instead of Thread.sleep(100);
    – Alice
    Dec 29 '18 at 9:50
















0














The two classes are not doing the same thing. You can probably reach the solution yourself by comparing them closely. First, do you know exactly how your first class (Marathon) works? In particular, what do you think the following line does?



t1.join(100);


The thread t1, which has just started running, has just gone into a loop which counts up once every 200 milliseconds. The join(100) call simply causes the current (main) thread to wait 100 milliseconds. You will achieve exactly the same results by replacing that line with this one:



Thread.sleep(100);



Now that the main thread has slept for 100 milliseconds, it starts thread t2. Now the two threads are running in parallel, and every 200 milliseconds both threads output a line, the second thread delayed by 100 milliseconds so that they appear evenly interleaved.



Now let's look at your second method, Marathon2. A few differences from the first class are immediately obvious:




  1. The sleep in the Runnable is outside the loop, instead of inside.

  2. The sleep in the Runnable is only 100 milliseconds, instead of 200.

  3. The maximum wait in the main thread is 500 milliseconds, instead of 100.

  4. The Future.get method causes a TimeoutException instead of just continuing. We can simply replace this call with a sleep anyway, since that's all that the first class does.


So, ironing out the differences, we get the following Marathon2 class which behaves in a similar manner to the other class (Marathon), with interleaved threads:



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
TimeUnit.MILLISECONDS.sleep(200);
}

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
TimeUnit.MILLISECONDS.sleep(100);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}





share|improve this answer





















  • Thanks you. I thought Future.get(long timeout, TimeUnit unit) is same as Thread.join(long millis). I could also use service.awaitTermination(100, TimeUnit.MILLISECONDS) instead of Thread.sleep(100);
    – Alice
    Dec 29 '18 at 9:50














0












0








0






The two classes are not doing the same thing. You can probably reach the solution yourself by comparing them closely. First, do you know exactly how your first class (Marathon) works? In particular, what do you think the following line does?



t1.join(100);


The thread t1, which has just started running, has just gone into a loop which counts up once every 200 milliseconds. The join(100) call simply causes the current (main) thread to wait 100 milliseconds. You will achieve exactly the same results by replacing that line with this one:



Thread.sleep(100);



Now that the main thread has slept for 100 milliseconds, it starts thread t2. Now the two threads are running in parallel, and every 200 milliseconds both threads output a line, the second thread delayed by 100 milliseconds so that they appear evenly interleaved.



Now let's look at your second method, Marathon2. A few differences from the first class are immediately obvious:




  1. The sleep in the Runnable is outside the loop, instead of inside.

  2. The sleep in the Runnable is only 100 milliseconds, instead of 200.

  3. The maximum wait in the main thread is 500 milliseconds, instead of 100.

  4. The Future.get method causes a TimeoutException instead of just continuing. We can simply replace this call with a sleep anyway, since that's all that the first class does.


So, ironing out the differences, we get the following Marathon2 class which behaves in a similar manner to the other class (Marathon), with interleaved threads:



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
TimeUnit.MILLISECONDS.sleep(200);
}

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
TimeUnit.MILLISECONDS.sleep(100);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}





share|improve this answer












The two classes are not doing the same thing. You can probably reach the solution yourself by comparing them closely. First, do you know exactly how your first class (Marathon) works? In particular, what do you think the following line does?



t1.join(100);


The thread t1, which has just started running, has just gone into a loop which counts up once every 200 milliseconds. The join(100) call simply causes the current (main) thread to wait 100 milliseconds. You will achieve exactly the same results by replacing that line with this one:



Thread.sleep(100);



Now that the main thread has slept for 100 milliseconds, it starts thread t2. Now the two threads are running in parallel, and every 200 milliseconds both threads output a line, the second thread delayed by 100 milliseconds so that they appear evenly interleaved.



Now let's look at your second method, Marathon2. A few differences from the first class are immediately obvious:




  1. The sleep in the Runnable is outside the loop, instead of inside.

  2. The sleep in the Runnable is only 100 milliseconds, instead of 200.

  3. The maximum wait in the main thread is 500 milliseconds, instead of 100.

  4. The Future.get method causes a TimeoutException instead of just continuing. We can simply replace this call with a sleep anyway, since that's all that the first class does.


So, ironing out the differences, we get the following Marathon2 class which behaves in a similar manner to the other class (Marathon), with interleaved threads:



public class Marathon2 {

public static void main(String args)
throws InterruptedException, ExecutionException, TimeoutException {

ExecutorService service = null;

Runnable task = () -> {
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " is running... " + i);
TimeUnit.MILLISECONDS.sleep(200);
}

} catch (InterruptedException e) {
}
};
try {
service = Executors.newFixedThreadPool(4);

Future<?> job1 = service.submit(task);
TimeUnit.MILLISECONDS.sleep(100);
Future<?> job2 = service.submit(task);
} finally {
if (service != null)
service.shutdown();
}
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 28 '18 at 15:44









DodgyCodeExceptionDodgyCodeException

3,3191424




3,3191424












  • Thanks you. I thought Future.get(long timeout, TimeUnit unit) is same as Thread.join(long millis). I could also use service.awaitTermination(100, TimeUnit.MILLISECONDS) instead of Thread.sleep(100);
    – Alice
    Dec 29 '18 at 9:50


















  • Thanks you. I thought Future.get(long timeout, TimeUnit unit) is same as Thread.join(long millis). I could also use service.awaitTermination(100, TimeUnit.MILLISECONDS) instead of Thread.sleep(100);
    – Alice
    Dec 29 '18 at 9:50
















Thanks you. I thought Future.get(long timeout, TimeUnit unit) is same as Thread.join(long millis). I could also use service.awaitTermination(100, TimeUnit.MILLISECONDS) instead of Thread.sleep(100);
– Alice
Dec 29 '18 at 9:50




Thanks you. I thought Future.get(long timeout, TimeUnit unit) is same as Thread.join(long millis). I could also use service.awaitTermination(100, TimeUnit.MILLISECONDS) instead of Thread.sleep(100);
– Alice
Dec 29 '18 at 9:50













1














Without dealing with any threads nor with Executors directly you can do it with a CompletableFuture



 Runnable runnable = () -> System.out.println("hi");
Runnable runnable1 = () -> System.out.println("there");
CompletableFuture<Void> all = CompletableFuture.runAsync(runnable).thenRun(runnable1);

all.whenComplete((x,th) -> {
System.out.println("both done");
});


Note that this would use the common ForkJoin pool but you can still provide your own.






share|improve this answer























  • Not quite the interleaved output the OP was looking for.
    – DodgyCodeException
    Dec 28 '18 at 12:58










  • I just found out ExecutorService.invokeAll(callable) also output sequential order.
    – Alice
    Dec 29 '18 at 9:49
















1














Without dealing with any threads nor with Executors directly you can do it with a CompletableFuture



 Runnable runnable = () -> System.out.println("hi");
Runnable runnable1 = () -> System.out.println("there");
CompletableFuture<Void> all = CompletableFuture.runAsync(runnable).thenRun(runnable1);

all.whenComplete((x,th) -> {
System.out.println("both done");
});


Note that this would use the common ForkJoin pool but you can still provide your own.






share|improve this answer























  • Not quite the interleaved output the OP was looking for.
    – DodgyCodeException
    Dec 28 '18 at 12:58










  • I just found out ExecutorService.invokeAll(callable) also output sequential order.
    – Alice
    Dec 29 '18 at 9:49














1












1








1






Without dealing with any threads nor with Executors directly you can do it with a CompletableFuture



 Runnable runnable = () -> System.out.println("hi");
Runnable runnable1 = () -> System.out.println("there");
CompletableFuture<Void> all = CompletableFuture.runAsync(runnable).thenRun(runnable1);

all.whenComplete((x,th) -> {
System.out.println("both done");
});


Note that this would use the common ForkJoin pool but you can still provide your own.






share|improve this answer














Without dealing with any threads nor with Executors directly you can do it with a CompletableFuture



 Runnable runnable = () -> System.out.println("hi");
Runnable runnable1 = () -> System.out.println("there");
CompletableFuture<Void> all = CompletableFuture.runAsync(runnable).thenRun(runnable1);

all.whenComplete((x,th) -> {
System.out.println("both done");
});


Note that this would use the common ForkJoin pool but you can still provide your own.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 10:05

























answered Dec 28 '18 at 9:47









Sleiman JneidiSleiman Jneidi

15.9k74362




15.9k74362












  • Not quite the interleaved output the OP was looking for.
    – DodgyCodeException
    Dec 28 '18 at 12:58










  • I just found out ExecutorService.invokeAll(callable) also output sequential order.
    – Alice
    Dec 29 '18 at 9:49


















  • Not quite the interleaved output the OP was looking for.
    – DodgyCodeException
    Dec 28 '18 at 12:58










  • I just found out ExecutorService.invokeAll(callable) also output sequential order.
    – Alice
    Dec 29 '18 at 9:49
















Not quite the interleaved output the OP was looking for.
– DodgyCodeException
Dec 28 '18 at 12:58




Not quite the interleaved output the OP was looking for.
– DodgyCodeException
Dec 28 '18 at 12:58












I just found out ExecutorService.invokeAll(callable) also output sequential order.
– Alice
Dec 29 '18 at 9:49




I just found out ExecutorService.invokeAll(callable) also output sequential order.
– Alice
Dec 29 '18 at 9:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53956312%2fthread-newbie-joining-thread-one-after-another-with-concurrent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas