Use threading to process file chunk by chunk

后端 未结 2 1372
感情败类
感情败类 2021-01-22 14:56

I have a arraylist which consists of 5000 IP Addresses. For each IP Address, I want to execute a SNMPGet request and a FTPDownload command. I want to implement it in a fashion,

2条回答
  •  无人共我
    2021-01-22 15:07

    Herewith adding working example having 5 threads. Just put the test.txt in your CLASS_PATH of the application.

    class MyRunnable implements Runnable {
    List> records;
    MyRunnable(List> records){
        this.records = records;
    }
    public void run(){
        for(List list : records){
            System.out.println(Thread.currentThread().getName() + " : "+list.toString());
        }
    }}
    

    Main Class -

    public class FileProcessThreads {
    public List> process(String fileName) throws IOException {
        List> records = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line = null;
        while((line = br.readLine()) != null){
            List listValues = Arrays.asList(line.split(" "));
            records.add(listValues);
        }
        System.out.println(records.size());
        return records;
    }
    public static void main(String[] args) throws IOException {
        FileProcessThreads fp = new FileProcessThreads();
        List> records = fp.process("test.txt");
        ExecutorService es = Executors.newFixedThreadPool(5);
        int recordsInEachThread = (int) (records.size()/5);
        System.out.println(recordsInEachThread);
    
        MyRunnable my1 = new MyRunnable(records.subList(0, recordsInEachThread));
        MyRunnable my2 = new MyRunnable(records.subList(recordsInEachThread+1, recordsInEachThread*2));
        MyRunnable my3 = new MyRunnable(records.subList(recordsInEachThread*2 + 1, recordsInEachThread*3));
        MyRunnable my4 = new MyRunnable(records.subList(recordsInEachThread*3 + 1, recordsInEachThread*4));
        MyRunnable my5 = new MyRunnable(records.subList(recordsInEachThread*4 + 1, records.size() - 1));
        es.execute(my1);
        es.execute(my2);
        es.execute(my3);
        es.execute(my4);
        es.execute(my5);
        es.shutdown();
    }}
    

提交回复
热议问题