ノードのセットにpingを実行し、文字列のArrayListを将来のオブジェクトに返してファイルに出力する単純なユーティリティがあります。プログラムは、ユーザーが終了するまで実行する必要があります。
future が結果を受け取る (または少なくとも結果をメソッドに渡してファイルに出力する) ようには見えません。同時に実行しているスレッドの数に関係なく (入力ファイルによって決定される常に 100 未満)、最初と最後に初期化されたスレッドからの結果のみを出力しています。
サニティ チェックとして、各スレッドが結果を送信してから閉じて Future オブジェクトに結果を返すグローバル変数を作成しました。この変数は、すべてのスレッドによって正しく更新されます。
Future がスレッドからすべての結果を受け取っていないように見える理由は誰にもありますか?
public class PingUtility{
public static ExecutorService pool = Executors.newFixedThreadPool(100);
static Future<ArrayList<String>> future;
public static void main(String[] args) throws Exception {
Timer timer = new Timer();
TimerTask task = new TimerTask(){
public void run(){
//Creates a pool of threads to be executed
ArrayList<String[]> nodes = new ArrayList<String[]>()
future = pool.submit(new PingNode(nodes));
}
}
};
timer.scheduleAtFixedRate(task, 0, interval);
while(true){
try{
ArrayList<String[]> tempOutputArray = future.get();
Iterator<String[]> it = tempOutputArray.iterator();
while(it.hasNext()) appendFile(it.next());
tempOutputArray.clear();
}catch(Exception nullException){
//Do nothing
}
}
}