3 Replies Latest reply: Apr 3, 2008 5:59 AM by 807591 RSS

    synchronizing thread output.

    807591
      Hi,
      I am trying to implement a simple class that act similar to the Fgrep command in unix.

      I have almost acheived what has been asked for, but the final step eludes me. I was wondering if any can point me int he right direction?

      This is what I have so far

      Sgrep.java looks like this, and seems to work fine by itself.

      import java.io.*;
      import java.lang.String;

      public class Sgrep
      {
      private String string, fileName;

      Sgrep(String searchString, String Filename)
      {
      string = searchString;
      fileName = Filename;
      }

      public String getFilename()
      {
      return fileName;
      }

      public String search()
      {
      if(string == null)
      return string;
      else
      {
      FileReader reader;
      try
      {
      reader = new FileReader(getFilename());
      }
      catch(FileNotFoundException e)
      {return getFilename();}

      LineNumberReader lineNumberReader = new LineNumberReader(reader);
      String line, answer = "";
      try
      {
      while ((line = lineNumberReader.readLine()) != null)
      {
      if( line.indexOf(string) != -1)
      {
      answer += "\n" + lineNumberReader.getLineNumber() + ":\t" + line.trim();
      }
      }
      return (answer == "" ? answer : answer + "\n");
      }
      catch(IOException e)
      {return answer;}
      }
      }

      public static void main(String[] args)
      {
      if(args.length != 2)
      {
      System.out.println("Usage: java Sgrep <string> <filename>");
      return;
      }
      Sgrep task = new Sgrep(args[0], args[1]);
      System.out.println(task.getFilename());
      System.out.println(task.search());
      }
      }



      Frgep.java looks like this, but the output (which will be shown at the end) is interwoven. I am told there are no fields in Fgrep and on the the constructor, run and main methods.

      public class Fgrep extends Sgrep implements Runnable
      {
      Fgrep(String searchString, String Filename)
      {
      super(searchString, Filename);
      }

      public void run()
      {
      System.out.println(getFilename());
      System.out.println(search());
      }

      public static void main(String[] args)
      {
      if(args.length < 2)
      {
      System.out.println("Usage: java Fgrep " +
      "<string> <filename1> ... <filenameN>");
      return;
      }

      for(int i = 1; i < args.length; i++)
      {
      Fgrep task = new Fgrep(args[0], args);
      new Thread(task).start();
      }
      }
      }



      My output is random, but generally like this:

      Fgrep.class
      Sgrep.class
      Fgrep.java

      Sgrep.java

      21: if(string == null)
      44: return (answer == "" ? answer : answer + "\n");


      When it should be like this:

      Fgrep.class

      Fgrep.java

      Sgrep.class

      Sgrep.java

      21: if(string == null)
      44: return (answer == "" ? answer : answer + "\n")


      If someone can help, it would be good. I am going bald pulling my hair out trying to figure it out.

      regards

      Radiation therapist
        • 1. Re: synchronizing thread output.
          807591
          How do people not see the big
           button above the area I'm typing this into...
          
          Haha, oh well.
          
          Please repost your code in code tags so it's worth reading.
          
          Do not just copy and paste from here, though, as that will just repost the unformatted code. Copy it from your IDE or notepad or whatever you originally wrote it in.
          
          Thanks & look forward to perhaps shedding some light on your problem.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
          • 2. Re: synchronizing thread output.
            807591
            Sorry. My first post to the forum

            Here is the Sgrep code, followed by the Fgrep code
            import java.io.*;
            import java.lang.String;
            
            public class Sgrep
            {
                 private String string, fileName;
            
                 Sgrep(String searchString, String Filename)
                 {
                      string = searchString;
                      fileName = Filename;
                 }
            
                 public String getFilename()
                 {
                      return fileName;
                 }
            
                 public String search()
                 {
                      if(string == null)
                           return string;
                      else
                      {
                           FileReader reader;
                           try
                           {
                                reader = new FileReader(getFilename());
                           }
                           catch(FileNotFoundException e)
                           {return getFilename();}
            
                           LineNumberReader lineNumberReader = new LineNumberReader(reader);
                           String line, answer = "";
                           try
                           {
                                while ((line = lineNumberReader.readLine()) != null)
                                {
                                     if( line.indexOf(string) != -1)
                                     {
                                          answer += "\n" + lineNumberReader.getLineNumber() + ":\t" + line.trim();
                                     }
                                }
                           return (answer == "" ? answer : answer + "\n");
                           }
                           catch(IOException e)
                           {return answer;}
            
                      }
                 }
            
                public static void main(String[] args){
                    if(args.length != 2){
                        System.out.println("Usage: java Sgrep <string> <filename>");
                        return; }
                    Sgrep task = new Sgrep(args[0], args[1]);
                    System.out.println(task.getFilename());
                    System.out.println(task.search());
                }
            }
            Fgrep
            public class Fgrep extends Sgrep implements Runnable
            {
                 Fgrep(String searchString, String Filename)
                 {
                      super(searchString, Filename);
                 }
            
                 public void run()
                 {
                      System.out.println(getFilename());
                      System.out.println(search());
                 }
            
                public static void main(String[] args)
                {
                    if(args.length < 2)
                    {
                           System.out.println("Usage: java Fgrep " +
                                "<string> <filename1> ... <filenameN>");
                           return;
                      }
            
                    for(int i = 1; i < args.length; i++)
                    {
                           Fgrep task = new Fgrep(args[0], args);
            new Thread(task).start();

            }
            }
            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
            • 3. Re: synchronizing thread output.
              807591
              Hi radiationtherapist,

              I only had a quick look at your code but if I understand you correct, the only problem is that the output of the different threads mix.

              I would suppose a solution like this:
              public void run()
                {
                String filename;
                String search_result;
              
                filename = getFilename();
                search_result = search();
              
                synchronized(Fgrep.class)
                  {
                  System.out.println(filename);
                  System.out.println(search_result);
                  }
                }
              Andreas