|
| 1 | +package com.stackify.threadpools; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.concurrent.Callable; |
| 5 | +import java.util.concurrent.ExecutionException; |
| 6 | +import java.util.concurrent.Executor; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.ForkJoinPool; |
| 10 | +import java.util.concurrent.Future; |
| 11 | +import java.util.concurrent.LinkedBlockingQueue; |
| 12 | +import java.util.concurrent.ScheduledExecutorService; |
| 13 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 14 | +import java.util.concurrent.ThreadPoolExecutor; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import com.stackify.models.Employee; |
| 21 | +import com.stackify.services.EmployeeService; |
| 22 | + |
| 23 | +public class ThreadsApplication { |
| 24 | + |
| 25 | + private static final Logger logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + testExecutor(); |
| 29 | + testExecutorService(); |
| 30 | + testScheduledExecutorService(); |
| 31 | + testThreadPoolExecutor(); |
| 32 | + testForkJoinPool(); |
| 33 | + } |
| 34 | + |
| 35 | + private static EmployeeService employeeService = new EmployeeService(); |
| 36 | + |
| 37 | + public static void testExecutor() { |
| 38 | + Executor executor = Executors.newSingleThreadExecutor(); |
| 39 | + executor.execute(() -> System.out.println("Single thread pool test")); |
| 40 | + } |
| 41 | + |
| 42 | + public static void testExecutorService() { |
| 43 | + |
| 44 | + Employee employee = new Employee("John", 2000); |
| 45 | + |
| 46 | + ExecutorService executor = Executors.newFixedThreadPool(10); |
| 47 | + |
| 48 | + Callable<Double> callableTask = () -> { |
| 49 | + return employeeService.calculateBonus(employee); |
| 50 | + }; |
| 51 | + Future<Double> future = executor.submit(callableTask); |
| 52 | + |
| 53 | + try { |
| 54 | + if (future.isDone()) { |
| 55 | + double result = future.get(); |
| 56 | + System.out.println("Bonus is:" + result); |
| 57 | + } |
| 58 | + } catch (InterruptedException | ExecutionException e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } |
| 61 | + |
| 62 | + executor.shutdown(); |
| 63 | + } |
| 64 | + |
| 65 | + public static void testScheduledExecutorService() { |
| 66 | + Employee employee = new Employee("John", 2000); |
| 67 | + |
| 68 | + ScheduledExecutorService executor = Executors.newScheduledThreadPool(10); |
| 69 | + |
| 70 | + Callable<Double> callableTask = () -> { |
| 71 | + return employeeService.calculateBonus(employee); |
| 72 | + }; |
| 73 | + |
| 74 | + Future<Double> futureScheduled = executor.schedule(callableTask, 2, TimeUnit.MILLISECONDS); |
| 75 | + |
| 76 | + try { |
| 77 | + System.out.println("Bonus:" + futureScheduled.get()); |
| 78 | + } catch (InterruptedException | ExecutionException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + |
| 82 | + executor.scheduleAtFixedRate(() -> System.out.println("Fixed Rate Scheduled"), 2, 2000, TimeUnit.MILLISECONDS); |
| 83 | + executor.scheduleWithFixedDelay(() -> System.out.println("Fixed Delay Scheduled"), 2, 2000, TimeUnit.MILLISECONDS); |
| 84 | + } |
| 85 | + |
| 86 | + public static void testThreadPoolExecutor() { |
| 87 | + ThreadPoolExecutor fixedPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10); |
| 88 | + ThreadPoolExecutor cachedPoolExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); |
| 89 | + |
| 90 | + ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 6, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); |
| 91 | + executor.setMaximumPoolSize(8); |
| 92 | + |
| 93 | + ScheduledThreadPoolExecutor scheduledExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(5); |
| 94 | + } |
| 95 | + |
| 96 | + public static void testForkJoinPool() { |
| 97 | + ForkJoinPool pool = ForkJoinPool.commonPool(); |
| 98 | + logger.info("Thread Pool Created"); |
| 99 | + BigInteger result = pool.invoke(new FactorialTask(100)); |
| 100 | + System.out.println(result.toString()); |
| 101 | + } |
| 102 | +} |
0 commit comments