久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用線程逐塊處理文件

Use threading to process file chunk by chunk(使用線程逐塊處理文件)
本文介紹了使用線程逐塊處理文件的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)包含 5000 個(gè) IP 地址的數(shù)組列表.對(duì)于每個(gè) IP 地址,我想執(zhí)行一個(gè) SNMPGet 請(qǐng)求和一個(gè) FTPDownload 命令.我想以一種方式實(shí)現(xiàn)它,其中前五個(gè) IP 地址同時(shí)運(yùn)行 2 個(gè)不同的線程.在執(zhí)行完這些 IP 地址之后,接下來(lái)的 2 個(gè) IP 地址將在這些線程上執(zhí)行.誰(shuí)能幫忙看看怎么弄?

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是一個(gè)擴(kuò)展線程的類,要實(shí)現(xiàn)的工作寫(xiě)在它的run()方法中.請(qǐng)幫忙.

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 將最適合您的解決方案.我在這里創(chuàng)建了一個(gè)示例.您可以根據(jù)需要增加線程數(shù).

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();
    }

}

這篇關(guān)于使用線程逐塊處理文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
URL Connection (FTP) in Java - Simple Question(Java 中的 URL 連接 (FTP) - 簡(jiǎn)單問(wèn)題)
主站蜘蛛池模板: 成人黄色电影在线播放 | 一区二区三区四区在线 | 国产资源在线视频 | 九九热精品视频 | 欧美一区二区三区的 | 久久精品一区二区三区四区 | 国产精品日日做人人爱 | 九九亚洲 | 亚洲天堂一区二区 | 亚洲精品乱码久久久久久按摩观 | 一区二区三区精品视频 | 天天操操 | 免费99视频 | 国产欧美一级二级三级在线视频 | 玖玖视频 | 日本精品视频 | 欧美成人hd| 亚欧洲精品在线视频免费观看 | 亚洲 欧美 另类 日韩 | 国产一区二 | 国产一区二区在线91 | 午夜成人免费视频 | 精品日韩欧美一区二区 | 最新国产精品精品视频 | 久久a久久 | 免费看一区二区三区 | av在线一区二区三区 | h片在线看 | 久久精品二区 | 麻豆一区二区三区 | 中文在线www | 欧美日韩成人 | 99免费精品视频 | 精品免费av | 国产视频一区二区 | 免费爱爱视频 | www.99热这里只有精品 | 国产一区二区三区四区五区加勒比 | 国产亚洲成av人片在线观看桃 | 午夜视频一区 | 国产一区二区黑人欧美xxxx |