88
99import org .apache .commons .lang3 .RandomUtils ;
1010import org .junit .After ;
11+ import org .junit .AfterClass ;
1112import org .junit .Before ;
13+ import org .junit .BeforeClass ;
1214import org .junit .Test ;
1315import org .slf4j .Logger ;
1416import org .slf4j .LoggerFactory ;
@@ -18,22 +20,34 @@ public class RunnableVsThreadTest {
1820 private static Logger log =
1921 LoggerFactory .getLogger (RunnableVsThreadTest .class );
2022
23+ private static ExecutorService executorService ;
24+
25+ @ BeforeClass
26+ public static void setup () {
27+ executorService = Executors .newCachedThreadPool ();
28+ }
29+
2130 @ Test
2231 public void givenARunnable_whenRunIt_thenResult () throws Exception {
2332 Thread thread = new Thread (new SimpleRunnable (
2433 "SimpleRunnable executed using Thread" ));
2534 thread .start ();
2635 thread .join ();
27-
28- ExecutorService executorService =
29- Executors .newCachedThreadPool ();
36+ }
37+
38+ @ Test
39+ public void givenARunnable_whenSubmitToES_thenResult () throws Exception {
3040
3141 executorService .submit (new SimpleRunnable (
3242 "SimpleRunnable executed using ExecutorService" )).get ();
43+ }
44+
45+ @ Test
46+ public void givenARunnableLambda_whenSubmitToES_thenResult ()
47+ throws Exception {
3348
3449 executorService .submit (()->
3550 log .info ("Lambda runnable executed!!!" )).get ();
36- executorService .shutdown ();
3751 }
3852
3953 @ Test
@@ -42,26 +56,39 @@ public void givenAThread_whenRunIt_thenResult() throws Exception{
4256 "SimpleThread executed using Thread" );
4357 thread .start ();
4458 thread .join ();
59+ }
60+
61+ @ Test
62+ public void givenAThread_whenSubmitToES_thenResult () throws Exception {
4563
46- ExecutorService executorService =
47- Executors .newCachedThreadPool ();
4864 executorService .submit (new SimpleThread (
4965 "SimpleThread executed using ExecutorService" )).get ();
5066 }
5167
5268 @ Test
53- public void givenACallable_whenRunIt_thenResult () throws Exception {
54- ExecutorService executorService =
55- Executors .newCachedThreadPool ();
69+ public void givenACallable_whenSubmitToES_thenResult () throws Exception {
5670
57- Future <Integer > future = executorService .submit (new SimpleCallable ());
71+ Future <Integer > future = executorService .submit (
72+ new SimpleCallable ());
5873 log .info ("Result from callable: {}" , future .get ());
74+ }
75+
76+ @ Test
77+ public void givenACallableAsLambda_whenSubmitToES_thenResult ()
78+ throws Exception {
5979
60- future = executorService .submit (() -> {
80+ Future < Integer > future = executorService .submit (() -> {
6181 return RandomUtils .nextInt (0 , 100 );
62- });
82+ });
83+
6384 log .info ("Result from callable: {}" , future .get ());
64-
85+ }
86+
87+ @ AfterClass
88+ public static void tearDown () {
89+ if ( executorService != null && !executorService .isShutdown ()) {
90+ executorService .shutdown ();
91+ }
6592 }
6693}
6794
@@ -107,4 +134,5 @@ public Integer call() throws Exception {
107134 return RandomUtils .nextInt (0 , 100 );
108135 }
109136
110- }
137+ }
138+
0 commit comments