I have one directory consisting of logfiles... I want to run 3 threads for m files where m is big. Here is my code in which i have created thread array and want to reuse the threads soon they finish the void run. But i am not able to use the threads again once they finish void run first time..here is my code anybody please help me.

import java.io.*;
import java.util.*;
import java.io.File;
import java.lang.Thread;
//import java.io.FileOutputStream;
//import java.nio.channels.FileLock;
//import java.util.concurrent.Executors;




   public class Getting extends Thread{

          static String buff3;

          public static void main(String[] args) throws Exception {


             Thread []  array = new Thread[3];


                Thread p1 = new Getting();
                Thread p2 = new Getting();
                Thread p3 = new Getting();


                    array[0]=p1;
                    array[1]=p2;
                    array[2]=p3;




                long start = System.nanoTime();


                //Input directory path
                File folder = new File("/home/cleartrip/Desktop/input");
                File[] listOfFiles = folder.listFiles();

                for (int t= 0; t <listOfFiles.length ; t++ ) 
                {

                  if (listOfFiles[t].isFile()) 
                  {
                    System.out.println();
                    System.out.print("FILE NAME    =\t");  
                    System.out.println(listOfFiles[t].getName());



                     String buff1 = listOfFiles[t].getName();
                     String buff2 = "/home/cleartrip/Desktop/input/";
                      buff3 = buff2.concat(buff1);

                     // System.out.print("path of the given file is =");
                     //System.out.println(buff3);

                      if(t<3)
                      {
                      int e = t;

                       try
                         {

                           array[e].start();

                         }

                         catch (Exception m)
                         {
                           System.out.println(m); 
                         }
                       System.out.println(array[e].getName()); 

                      }

                      else
                      {
                          int e = t%3;


                          do
                          {
                            sleep(1000);
                            //array[e].join();

                          }while((!array[e].isAlive()));

                          try
                          {
                          array[e].start();
                          }
                          catch(Exception ab)
                          {
                             System.out.println(ab); 
                          }
                          System.out.println(array[e].getName()); 



                      }

                 }

                }

                long end = System.nanoTime();
                long elapsedTime = end - start;

                System.out.println();
                System.out.print(elapsedTime); 


      }



             public void run()

            {


                 String[] arr = new String[99999];

                 int i=0;
                 File f= new File(buff3);


                 if(!f.exists() && f.length()<0)
                 System.out.println("The specified file is either not exist or doesn't contain data");
                 else{
                     try{

                BufferedReader bufRdr  = new BufferedReader(new FileReader(f));
                String line= null;



        //Reading the ip address from the file storing it in an array called arr[] : array of strings               
                while((line = bufRdr.readLine()) != null)

                {
                    StringTokenizer st = new StringTokenizer(line," ");

                    arr[i++]=st.nextToken();
                }
                }catch(Exception e){
                    System.out.println("error");

                }

        }




        //sorting of IP addresses which are stored in araray named arr string type          

                    int n=i;

                    int a,b;

                    String temp;

                    int sortTheStrings = n-1;

                    for (a = 0; a < sortTheStrings; ++a)

                    for (b = 0; b < sortTheStrings; ++b)

                    if(arr[b].compareTo(arr[b + 1]) >0)

                    {

                    temp = arr[b];

                    arr[b] = arr[b + 1];

                    arr[b + 1] = temp;

                    }


// module to print the contents of the array i,e listing of how many IP addresses appeared how many times in the sorted array.


            System.out.print("IP ADDRESS ");
            System.out.print("\t\t");
            System.out.print("NO. OF TIMES APPEARED");      

                  int count;

                  for(int j=0;j<n;j=j+count)

                {
                    count=1;
                   if(j<n-1)
                   {       
                     while(arr[j].equals(arr[j+1]))
                   {
                      count++;
                      j++;

                   } 


                if(count>=2)
                    for(int k=count;k>=2;k--) j--;
                   System.out.println();
                   System.out.print(arr[j]);
                   System.out.print("\t\t");
                   System.out.print(count);
                   System.out.print("\n");

                  }
                }





        }   

}

No, you can't. You need to either create a new thread for each file, which is wasteful, or create and pass a list of files, rather than just one. Or, have the thread, essentially, ask for the next file (i.e. call a method in the "main" class to retreive the next file to work on).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.