1+ package com .baeldung .concurrent .runnable ;
2+
3+ import java .util .concurrent .Callable ;
4+ import java .util .concurrent .ExecutionException ;
5+ import java .util .concurrent .ExecutorService ;
6+ import java .util .concurrent .Executors ;
7+ import java .util .concurrent .Future ;
8+
9+ import org .apache .commons .lang3 .RandomUtils ;
10+ import org .junit .After ;
11+ import org .junit .Before ;
12+ import org .junit .Test ;
13+ import org .slf4j .Logger ;
14+ import org .slf4j .LoggerFactory ;
15+
16+ public class RunnableVsThreadTest {
17+
18+ private static Logger log =
19+ LoggerFactory .getLogger (RunnableVsThreadTest .class );
20+
21+ @ Test
22+ public void givenARunnable_whenRunIt_thenResult () throws Exception {
23+ Thread thread = new Thread (new SimpleRunnable (
24+ "SimpleRunnable executed using Thread" ));
25+ thread .start ();
26+ thread .join ();
27+
28+ ExecutorService executorService =
29+ Executors .newCachedThreadPool ();
30+
31+ executorService .submit (new SimpleRunnable (
32+ "SimpleRunnable executed using ExecutorService" )).get ();
33+
34+ executorService .submit (()->
35+ log .info ("Lambda runnable executed!!!" )).get ();
36+ executorService .shutdown ();
37+ }
38+
39+ @ Test
40+ public void givenAThread_whenRunIt_thenResult () throws Exception {
41+ Thread thread = new SimpleThread (
42+ "SimpleThread executed using Thread" );
43+ thread .start ();
44+ thread .join ();
45+
46+ ExecutorService executorService =
47+ Executors .newCachedThreadPool ();
48+ executorService .submit (new SimpleThread (
49+ "SimpleThread executed using ExecutorService" )).get ();
50+ }
51+
52+ @ Test
53+ public void givenACallable_whenRunIt_thenResult () throws Exception {
54+ ExecutorService executorService =
55+ Executors .newCachedThreadPool ();
56+
57+ Future <Integer > future = executorService .submit (new SimpleCallable ());
58+ log .info ("Result from callable: {}" , future .get ());
59+
60+ future = executorService .submit (() -> {
61+ return RandomUtils .nextInt (0 , 100 );
62+ });
63+ log .info ("Result from callable: {}" , future .get ());
64+
65+ }
66+ }
67+
68+ class SimpleThread extends Thread {
69+
70+ private static final Logger log =
71+ LoggerFactory .getLogger (SimpleThread .class );
72+
73+ private String message ;
74+
75+ public SimpleThread (String message ) {
76+ this .message = message ;
77+ }
78+
79+ @ Override
80+ public void run () {
81+ log .info (message );
82+ }
83+ }
84+
85+ class SimpleRunnable implements Runnable {
86+
87+ private static final Logger log =
88+ LoggerFactory .getLogger (SimpleRunnable .class );
89+
90+ private String message ;
91+
92+ public SimpleRunnable (String message ) {
93+ this .message = message ;
94+ }
95+
96+
97+ @ Override
98+ public void run () {
99+ log .info (message );
100+ }
101+ }
102+
103+ class SimpleCallable implements Callable <Integer > {
104+
105+ @ Override
106+ public Integer call () throws Exception {
107+ return RandomUtils .nextInt (0 , 100 );
108+ }
109+
110+ }
0 commit comments