1import java.util.concurrent.ExecutorService;
2import java.util.concurrent.Executors;
3import java.util.concurrent.TimeUnit;
4
5public class TestThread {
6
7 public static void main(final String[] arguments) throws InterruptedException {
8 ExecutorService executor = Executors.newSingleThreadExecutor();
9
10 // very simple example, check the website for more info:
11 // https://www.tutorialspoint.com/java_concurrency/concurrency_newsinglethreadexecutor.htm
12
13 executor.submit(new Task());
14 System.out.println("Shutdown executor");
15 executor.shutdown();
16 }
17 }
18
19 static class Task implements Runnable {
20
21 public void run() {
22
23 try {
24 Long duration = (long) (Math.random() * 20);
25 System.out.println("Running Task!");
26 TimeUnit.SECONDS.sleep(duration);
27 } catch (InterruptedException e) {
28 e.printStackTrace();
29 }
30 }
31 }
32}