使用线程逐块处理文件

Use threading to process file chunk by chunk(使用线程逐块处理文件)
本文介绍了使用线程逐块处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个包含 5000 个 IP 地址的数组列表.对于每个 IP 地址,我想执行一个 SNMPGet 请求和一个 FTPDownload 命令.我想以一种方式实现它,其中前五个 IP 地址同时运行 2 个不同的线程.在执行完这些 IP 地址之后,接下来的 2 个 IP 地址将在这些线程上执行.谁能帮忙看看怎么弄?

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, wherein at a time 2 different threads run simultaneously for the first five IP Addresses. After the execution of these IP Addresses, next 2 IP Addresses will be executed on these threads. Can anyone help how to do it?

这里的connection是一个扩展线程的类,要实现的工作写在它的run()方法中.请帮忙.

Here, connection is a class which extends the thread and the work to be implemented is written in its run() method. Please help.

Connection newConnection =new Connection(0);
Connection newConnection1 =new Connection(1);

for(int i = 0; i < NE_list.getRowCount(); i=i+2)
{
if(NE_list.getValueAt(i, 0).toString().equals("true")) //Some condition here for the IP Address
{

            newConnection.i=i;
            newConnection1.i=i+1;
            newConnection.runprogram();
            newConnection1.runprogram();
 }


    } 

class Connection extends Thread{
int  i;
Connection(int val){
  i=val;
}
void runprogram(){
start();
}
public void run(){
//SNMP and FTP Code here for IP Address in index i of NE_list
}
}

推荐答案

Executor Framework 将最适合您的解决方案.我在这里创建了一个示例.您可以根据需要增加线程数.

Executor Framework will be best suit for your solution. I have created one example here. You can increase the number of threads as per your requirement.

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

class SomeRunnable implements Runnable {
    int threadNo = -1 ;
    List<String> list = new ArrayList<String>();
    public SomeRunnable(List list, int threadNo ) {
        this.list.addAll(list);
        this.threadNo =threadNo;
    }
    @Override
    public void run() {
        for (String element : list) {
            System.out.println("By Thread:" + threadNo+", Processed Element:" +element);
        }
    }

}

public class ExecutorDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            list.add("Elem:"+i);
        }
        // Divide list 
        int divideIndex = list.size()/2;
        //Create objects of Runnable
        SomeRunnable obj1 = new SomeRunnable(list.subList(0, divideIndex),1);
        SomeRunnable obj2 = new SomeRunnable(list.subList(divideIndex,list.size()),2);

        //Create fixed Thread pool, here pool of 2 thread will created
        ExecutorService pool = Executors.newFixedThreadPool(2);

        pool.execute(obj1);
        pool.execute(obj2);

        pool.shutdown();
    }

}

这篇关于使用线程逐块处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android sdk 18 TextView gravity doesn#39;t work vertically(Android sdk 18 TextView 重力不能垂直工作)
Java android: appending a newline using TextView(Java android:使用 TextView 添加换行符)
How to create simple Android TextView and display text on it using java code?(如何使用 java 代码创建简单的 Android TextView 并在其上显示文本?)
Add opaque quot;shadowquot; (outline) to Android TextView(添加不透明的“阴影(大纲)到 Android TextView)
Android: Creating a Circular TextView?(Android:创建一个圆形 TextView?)
Android - set TextView TextStyle programmatically?(Android - 以编程方式设置 TextView TextStyle?)