久久久久久久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) - 簡單問題)
主站蜘蛛池模板: 黑人黄色一级片 | 精品粉嫩小bbwbbwbbw | 激情六月天 | 日本a在线观看 | 97在线免费观看视频 | 谁有毛片网址 | 在线观看视频一区二区 | 久久久久久久免费视频 | 国产欧美一区二区精品忘忧草 | 成人高潮片免费网站 | 午夜精品久久久久久 | 狠狠撸狠狠干 | 他揉捏她两乳不停呻吟动态图 | 国产一区二区观看 | 免费福利片 | 午夜在线视频观看日韩17c | 午夜成人影片 | 中文字幕免费 | 男女久久久 | 91午夜理伦私人影院 | 欧洲性视频 | 欧美日韩在线视频观看 | 99超碰在线观看 | 亚洲精品视频一区 | 婷婷狠狠 | 干干干操操操 | 成人高潮片免费视频 | 国产乱码一区二区三区 | 日韩欧美一区在线 | 日日操日日射 | 国产成人免费观看 | 亚洲免费视频观看 | 91片黄在线观看动漫 | 久久久久久久久久国产精品 | 国产一级二级 | 久久精品导航 | 日本一级大毛片a一 | 手机av在线免费观看 | 在线国产91| 国产美女精品 | 在线观看免费黄色 |