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

使用線程逐塊處理文件

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

}

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

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
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 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
URL Connection (FTP) in Java - Simple Question(Java 中的 URL 連接 (FTP) - 簡單問題)
主站蜘蛛池模板: 韩国av电影网 | 美国av毛片| 欧美综合一区 | 欧美精品久久久久 | 免费簧片视频 | 日日骚网 | 欧美一级一区 | 亚洲福利| 国产精品久久久久久一区二区三区 | 亚洲 欧美 日韩 精品 | 91精品一区二区三区久久久久 | 国产成人91视频 | 日韩中文欧美 | av免费网站在线观看 | 久久国产精品一区 | 国产成人免费在线 | 久久成人国产 | 日韩国产欧美在线观看 | 国产视频亚洲视频 | 麻豆精品国产免费 | 欧美中文视频 | 国产成人精品久久二区二区 | h视频亚洲| 成人免费视频网站在线看 | 黄色片免费看 | 亚洲午夜精品在线观看 | 中文天堂在线一区 | 欧美乱人伦视频 | 亚洲视频三区 | 天天躁日日躁狠狠的躁天龙影院 | 亚洲网址在线观看 | 精品国产91乱码一区二区三区 | 亚洲成a| 国产精品成人在线播放 | 亚洲国产精品美女 | 亚洲婷婷六月天 | av在线黄 | 久久亚洲国产 | 天天操伊人 | 日韩欧美在线观看视频 | 91pron在线|