我有一个Java程序来读取输入文件(input.txt)并打印该文件的内容(此后,我需要在那做更多工作,即ssh到计算机并检查状态,发送电子邮件等。)由于input.txt文件包含更多行,因此大约需要3个小时才能完成程序。

因此,喜欢在Java(例如线程)中使用spawn概念或其他某种技术来在读取输入文件的同时溢出进程,同时执行其他工作(ssh,checkstatus,发送邮件),这样程序几乎不会花费十分钟的时间完成程序。

我是Java的新手。您能否指导我如何提出这一逻辑。我已经将代码粘贴到这里了。

input.txt文件示例:


  #

    ABC

  
  #
  
  大男孩72.24.1 72.24.157.57
  劫持2.24.157.97 1.24.157.69
  波尼2.24.147.96 9.24.159.86
  具有讽刺意味的7.24.145.93 8.24.209.55
  
  #

   xyz

  
  #
  
  阿尔卑斯山2.24.140.199 1.24.140.46


Java程序:

import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.util.Calendar;
import java.net.*;
import java.net.UnknownHostException;


public class panic_email{
    public static void main(String[] args) throws IOException{
    try{
        System.out.println ("Starting of program...");

        FileInputStream fstream = new FileInputStream("input.txt");
        DataInputStream input = new DataInputStream(fstream);
        BufferedReader bfr = new BufferedReader(new InputStreamReader(input));
        String Name = "";
        String IP = "";

        CSLOOP: while ((FileLine = bfr.readLine()) != null)   {
        FileLine = FileLine.trim();
        if ( FileLine.startsWith("XYZ") ){
                System.out.println ("End of program");
                break;
        }
        if ( !FileLine.startsWith("#") && !FileLine.startsWith(" ") ){
          String splitLine[] = null;
          splitLine = FileLine.split("\\s+");
          if( splitLine.length >= 3){
          Name = splitLine[0];
          Ip = splitLine[2];
System.out.println("Name:" + Name + "IP" + Ip);
//Here after this I am doing some kind of extra work like ssh,checkstate,send email. So while coming to this point I need to spawn the process i think.
         }
         }

 }

}


你能指导我怎么做吗

谢谢,
肋骨

最佳答案

Java对多线程有很好的支持。请阅读Concurrency Section from Java Tutorial

关于java - 如何产生一个Java程序以减少运行时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3685833/

10-13 09:12