Threading Demo

 avatar
unknown
java
a year ago
1.4 kB
5
Indexable
package com.booking.flights.ancillarycontent.service;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadingDemo {

    public static void main(String[] args) throws InterruptedException {
        new ThreadingDemo().start();
    }
    void start() throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        executor.submit(()-> task());
        executor.submit(()-> task());
        executor.submit(()-> task());
        executor.submit(()-> task());
        executor.submit(()-> task());
        executor.submit(()-> task());
        executor.submit(()-> task());
        executor.submit(()-> task());
        executor.submit(()-> task());
        ;
        List<Callable<Method>> callables = Arrays.asList(new Callable<Method>() {
            @Override
            public Method call() throws Exception {
                System.out.println("Finished Thread Execution");
                return null;
            }
        });
        executor.invokeAll(callables);
    }
    void task(){
        try {
            System.out.println("Executing thread ...");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}
Editor is loading...
Leave a Comment