Thread does a random access of file, notify a function when criteria is met
<p>I am setting up a dial up connection in Linux using a utility. I write the output of the utility which is terminal based into a log file. Parallelly i start a thread which is reading the contents of the file(using RandomAccessFile object). I am checking for some connection strings inside the log to conclude about successful connection or disconnection.
Here is the thread code which does the reading from file and prints it at the same time.</p>

<pre><code>try
    {
      this.tailing = true;
      RandomAccessFile file = new RandomAccessFile( logfile, "r" );
      while( this.tailing )
      {
        try
        {  
          long fileLength = this.logfile.length();
          if( fileLength &lt; filePointer ) 
          {
             file = new RandomAccessFile( logfile, "r" );
            filePointer = 0;
          }

          if( fileLength &gt; filePointer ) 
          {
            // There is data to read
            file.seek( filePointer );
            String line = file.readLine();
            while( line != null )
            {
              this.fireNewLogFileLine( line );
              line = file.readLine();

              System.out.println("Print the line:"+line);
            }
            filePointer = file.getFilePointer();
          }

          // Sleep for the specified interval
          sleep( this.sampleInterval );
        }
        catch( InterruptedException e )
        {
        }
      }
</code></pre>

<p>I have a function which actually does the connection to the linux utility the code is as follows...</p>

<pre><code>try{


            //Runtime rt = Runtime.getRuntime();
               Process proc = Runtime.getRuntime().exec("Command to issue the connect command");
               InputStream stderr = proc.getErrorStream();
               InputStreamReader isr = new InputStreamReader(stderr);
               BufferedReader br = new BufferedReader(isr);

                String line = null;
                //System.out.println("&lt;ERROR&gt;");

                            OutputStream out = new FileOutputStream("File path to log the output of the connect command");
                            int read = 0;
                            byte[] bytes = new byte[1024];

                            while((read=stderr.read(bytes))!=-1){
                                out.write(bytes,0,read);
                                }

                            out.flush();
                            out.close();

                    while ( (line = br.readLine()) != null){
                        System.out.println(line);

                    }
                    //System.out.println("&lt;/ERROR&gt;");
                    int exitVal = proc.waitFor();
                    //System.out.println("Process exitValue: " + exitVal);
                    logger.info("Process exitvalue:" +exitVal);

                // Here we will check the log file to know if the connection is successful 
                // and appropriately update the boolean flag



        }catch(Throwable t){
         t.printStackTrace();
        }
</code></pre>

<p>In the above function i would like to start the thread and appropriately updat ethe connection status.</p>

<p>Does this function needs to be in a Parent Thread? How can i call that thread which i wrote to return me the conenction status.</p>
