如何创建10个ID的线程,使用多线程在Java中并行运行6个方法

问题描述 投票:-3回答:1

我在列表中有10个ID,还有6种方法。在6种方法中我需要每个ID。

示例:

//在所有6种方法中传递第一个id,这6种方法应并行运行

getBook1(1);
getBook2(1);
getBook3(1);
getBook4(1);
getBook5(1);
getBook6(1);

//在所有6种方法中传递第二个ID

getBook1(2);
getBook2(2);
getBook3(2);
getBook4(2);
getBook5(2);
getBook6(2);
    .
    .
    .

//在所有6种方法中传递第十个ID

getBook1(10);
getBook2(10);
getBook3(10);
getBook4(10);
getBook5(10);
getBook6(10);

当我在所有6种方法中均通过第二个id时,并行的1个id方法也应并行运行。就像所有这6个方法的10个id一样,它们应在java中并行运行。请帮助我解决。

感谢您的回复。请检查下面的更新代码。

public class TestClass {
DataSource ds = null;
Connection mysqlcon = null;
PreparedStatement mysqlstmt = null;
ResultSet mysqlrs=null;

public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
    TestClass tc = new TestClass();
    List<String> idsList  = tc.getIpadds();
    ExecutorService executorService =  Executors.newFixedThreadPool(idsList .size());
    Method[] methods = TestClass.class.getMethods();

    for (String id : idsList ) {
        callbookMethod(executorService,id,methods);
    }

}

private List<String> getIpadds() {
    List<String> ipadds = new ArrayList<String>();
    try {
            // MySql Dash Board Connection
            ds = MyDataSourceFactory.getDashBoardMySQLDataSource();
            mysqlcon = ds.getConnection();
            String sql="SELECT DISTINCT IPADD FROM atm_master_copy_3";
            mysqlstmt = mysqlcon.prepareStatement(sql);
            ResultSet mysqlrs =mysqlstmt.executeQuery();

            while(mysqlrs.next()) {
                ipadds.add(mysqlrs.getString(1));
            }

        }catch(SQLException e) {
                e.printStackTrace();
            }finally {
                if(mysqlcon != null) {
                    try {
                        mysqlcon.close();
                    }catch(SQLException e) {
                        e.printStackTrace();
                    }
                    mysqlcon = null;
                }
            }
    return ipadds;
}

public static void callbookMethod (ExecutorService executorService,final String id, final Method[] methods){
    final TestClass testClass = new TestClass();
    executorService.execute(new Runnable() {
        @Override
        public void run() {
            for (Method method : methods) {
                try {
                    String methodName  = method.getName();
                    if (methodName.startsWith("update")) {
                            method.invoke(testClass,id);
                   }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

public void updateCMaster(String id)  {
     System.out.println("updateCMaster");

}
public void updateFMaster(String id) {
    // Do Something
    System.out.println("updateFMaster");
}
public void updateASMaster(int id) {
    // Do Something
    System.out.println("updateASMaster");
}

}

我在列表中获得250个ID。

java multithreading multitasking
1个回答
0
投票

在这里您可以完成您所要求的操作,但这不是一个好主意,因为如果列表很大,您将杀死您的计算机:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestClass {


    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {

        int[] idsList = {1,2,3,4,5,6,7,8,9,10};
        ExecutorService executorService =  Executors.newFixedThreadPool(idsList.length);
        Method[] methods = TestClass.class.getMethods();

        for (int id : idsList) {
            callbookMethod(executorService,id,methods);
        }

    }

    public static void callbookMethod (ExecutorService executorService,int id, Method[] methods){
        TestClass testClass = new TestClass();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                for (Method method : methods) {
                    try {
                        String methodName  = method.getName();
                        if (methodName.startsWith("getBook")) {
                            method. invoke(testClass,id);
                        }
//                        method.invoke(id);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void getBook1(int id)  {
        // Do Something
        System.out.println("Book 1:");
    }
    public void getBook2(int id) {
        // Do Something
        System.out.println("Book 2:");
    }
    public void getBook3(int id) {
        // Do Something
        System.out.println("Book 3:");
    }
    public void getBook4(int id) {
        // Do Something
        System.out.println("Book 4:");
    }
    public void getBook5(int id) {
        // Do Something
        System.out.println("Book 5:");
    }
    public void getBook6(int id) {
        // Do Something
        System.out.println("Book 6:");
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.