开发者

how to spawn a java program to reduce the running time

I have a java program to read a input file (input.txt) and print the content of that file (after that i need to do some more work there i.e ssh to a machine and check status,send email etc..). Since the input.txt file is having more lines it takes around 3hrs to complete the program.

So like to use spawn concept in ja开发者_Python百科va (like thread) or some other technique to spilt the process while reading the input file and do the other work (ssh,checkstatus,send mail) in the same time sothat the program willl take hardly ten mintues to finish the program.

I am newbie to Java. Could you please guide me how to put that logic. I have pasted the code here which I have right now.

Example of input.txt file:

#
    ABC
#

bigboy 72.24.1 72.24.157.57

bejack 2.24.157.97 1.24.157.69 boni 2.24.147.96 9.24.159.86 irony 7.24.145.93 8.24.209.55

#
   xyz
#

alches 2.24.140.199 1.24.140.46

Java program:

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. 
         }
         }

 }

}

Could you please guide me how to do this

Thanks, Ricks


Java has great support for multi-threading. Please read the Concurrency Section from Java Tutorial


Don't extend Thread unless you intend to change the way Thread works. Implement Runnable instead. You may also want to look into thread pooling (see Executors).

Basically you should create a class that implements Runnable and does the time consuming work in its run() method. Try and make this class encapsulate all the work required in an individual task (ssh, checkstatus, send mail). Your program could read through the text file and create instances of this task class and submit them to a thread pool.

Beware that you may need to throttle your program depending on;
a) how much system resources you have
b) how much system resources each task requires
c) how many tasks you will have running

A simple way of throttling may be to use a thread pool of a fixed size.


It is pretty easy to add multi treading support for your java application. The Tread class can help you with this. See http://www.javabeginner.com/learn-java/java-threads-tutorial for an easy tutorial.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜