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

使用線程逐塊處理文件

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

問題描述

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

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是一個擴展線程的類,要實現(xiàn)的工作寫在它的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 將最適合您的解決方案.我在這里創(chuàng)建了一個示例.您可以根據(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();
    }

}

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

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(liá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,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
URL Connection (FTP) in Java - Simple Question(Java 中的 URL 連接 (FTP) - 簡單問題)
主站蜘蛛池模板: 欧美黑人体内she精在线观看 | 无码日韩精品一区二区免费 | 天天操人人干 | 羞羞视频网站在线观看 | 天天拍天天色 | 国产成人精品午夜视频免费 | 日韩精品免费视频 | 免费观看a级毛片在线播放 黄网站免费入口 | 亚洲欧美日韩精品久久亚洲区 | 中文字幕在线观看第一页 | 嫩草影院网址 | 亚洲成人一区二区在线 | 欧美伊人久久久久久久久影院 | 亚洲午夜小视频 | 99精品一区二区三区 | 久久精品高清视频 | 亚洲福利免费 | 黄色毛片免费看 | 欧美日产国产成人免费图片 | 日本在线视频一区二区 | 国产精品久久久久久久久 | 日韩免费一级 | 成人午夜电影网 | 久久另类视频 | 一区二区三区精品视频 | 99久久成人| 亚洲精品一区二区三区四区高清 | 亚洲国产成人av好男人在线观看 | 狠狠干av | 日韩理论电影在线观看 | 亚洲精品一区二区三区四区高清 | 欧美激情在线精品一区二区三区 | 成人国产精品色哟哟 | 特黄视频| 亚洲成人精品 | 黄色毛片在线观看 | 亚洲国产精品日本 | 丁香婷婷久久久综合精品国产 | 精品国产乱码久久久久久影片 | 免费v片在线观看 | 在线看黄免费 |