如何在一种方法中使用多个Executor服务?

时间:2018-12-29 13:37:54

标签: java multithreading threadpool

这是我的示例,但未执行。仅正确地为我的第一个线程池工作,但第二个线程池未运行。它是否正确,如果它正确,我在哪里错误,如果不是我如何实现多个执行程序服务。在我的示例中,保存API成功执行,但预订约会方法book API成功,但是Book API无法执行后,我想知道这种行为

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyTestClass {

    public static void main(String args[]) {
        //Create class object
        MyTestClass  myObj = new MyTestClass();
        // Create List A
        List<String> a = new ArrayList<>();
        a.add("Test1");
        a.add("Test2");
        a.add("Test3");
        a.add("Test4");
        //Create List B
        List<String> b = new ArrayList<>();
        b.add("Test1");
        b.add("Test2");
        b.add("Test3");
        b.add("Test4");
        b.add("Test5");
        b.add("Test6");
        b.add("Test7");
        b.add("Test3");
        b.add("Test4");
        b.add("Test5");
        b.add("Test6");
        b.add("Test7");
        //Call my method
        myObj.myMethod(a, b);

    }
    //Multi-threading example in appointment book method execute first api but its not called the second api
    void myMethod(List<String> listA, List<String> listB){
        ExecutorService threadPool = Executors.newFixedThreadPool(listA.size());
            List<Callable<Object>> tasks = new ArrayList<>();
            for (String strA : listA) {
                tasks.add(Executors.callable(new Thread() {
                    @Override
                    public void run() {
                        processA(strA);     
            }
                }));
            }
            try {
                //Execute first thread pool for save
                threadPool.invokeAll(tasks);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
                System.out.println("Error while running threads");
            }
            threadPool.shutdown();
            //Call book appointment api
            bookAppointMent(listB);

    }

    //Book appointment
    void bookAppointMent(List<String> listB) {
         ExecutorService threadPool2 = Executors.newFixedThreadPool(listB.size());
            List<Callable<Object>> tasks2 = new ArrayList<>();
            for (String strB : listB) {
                tasks2.add(Executors.callable(new Thread() {
                    @Override
                    public void run() {
                       // Add task
                        processB(strB);     
            }
                }));
            }
            try {
                //Execute thread pool2
                threadPool2.invokeAll(tasks2);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
                System.out.println("Error while running threads");
            }
             //Shutdown pool-2
            threadPool2.shutdown();

    }

    //Save appointment
    public void processA(String a){
        //Here its save API
        System.out.println("Save A successfully...." + a);
    }

    //Book Appointment In this method only first api called but second one is not called
    public void processB(String b){
        //Here is First API call - This only called
        System.out.println("Appointment booked successfully...." + b);
        //Here is second API call - Its not called
        processAfterAppBooked(b);
    }

    //Process after book app
    public void processAfterAppBooked(String b) {
        //Second api call - its not execute
        System.out.println("After appointment booked " + b);
    }
}

0 个答案:

没有答案