forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding code for BAEL-1262 (eugenp#2866)
* adding code for BAEL-1262 * fixing the build issue by moving the test to test folder
- Loading branch information
1 parent
8248a66
commit 080af1c
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...java-concurrency/src/test/java/com/baeldung/concurrent/runnable/RunnableVsThreadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.baeldung.concurrent.runnable; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
|
||
import org.apache.commons.lang3.RandomUtils; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class RunnableVsThreadTest { | ||
|
||
private static Logger log = | ||
LoggerFactory.getLogger(RunnableVsThreadTest.class); | ||
|
||
@Test | ||
public void givenARunnable_whenRunIt_thenResult() throws Exception{ | ||
Thread thread = new Thread(new SimpleRunnable( | ||
"SimpleRunnable executed using Thread")); | ||
thread.start(); | ||
thread.join(); | ||
|
||
ExecutorService executorService = | ||
Executors.newCachedThreadPool(); | ||
|
||
executorService.submit(new SimpleRunnable( | ||
"SimpleRunnable executed using ExecutorService")).get(); | ||
|
||
executorService.submit(()-> | ||
log.info("Lambda runnable executed!!!")).get(); | ||
executorService.shutdown(); | ||
} | ||
|
||
@Test | ||
public void givenAThread_whenRunIt_thenResult() throws Exception{ | ||
Thread thread = new SimpleThread( | ||
"SimpleThread executed using Thread"); | ||
thread.start(); | ||
thread.join(); | ||
|
||
ExecutorService executorService = | ||
Executors.newCachedThreadPool(); | ||
executorService.submit(new SimpleThread( | ||
"SimpleThread executed using ExecutorService")).get(); | ||
} | ||
|
||
@Test | ||
public void givenACallable_whenRunIt_thenResult() throws Exception { | ||
ExecutorService executorService = | ||
Executors.newCachedThreadPool(); | ||
|
||
Future<Integer> future = executorService.submit(new SimpleCallable()); | ||
log.info("Result from callable: {}", future.get()); | ||
|
||
future = executorService.submit(() -> { | ||
return RandomUtils.nextInt(0, 100); | ||
}); | ||
log.info("Result from callable: {}", future.get()); | ||
|
||
} | ||
} | ||
|
||
class SimpleThread extends Thread{ | ||
|
||
private static final Logger log = | ||
LoggerFactory.getLogger(SimpleThread.class); | ||
|
||
private String message; | ||
|
||
public SimpleThread(String message) { | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
log.info(message); | ||
} | ||
} | ||
|
||
class SimpleRunnable implements Runnable { | ||
|
||
private static final Logger log = | ||
LoggerFactory.getLogger(SimpleRunnable.class); | ||
|
||
private String message; | ||
|
||
public SimpleRunnable(String message) { | ||
this.message = message; | ||
} | ||
|
||
|
||
@Override | ||
public void run() { | ||
log.info(message); | ||
} | ||
} | ||
|
||
class SimpleCallable implements Callable<Integer> { | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
return RandomUtils.nextInt(0, 100); | ||
} | ||
|
||
} |