Results 1 to 9 of 9
  1. #1
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default read from file and threading

    hello all. I'm still kind of new to java and have a question. I have a program that reads a text file. This program works fine if the text file is small but large text files cause the program to freeze up. So, im trying to change my program to have the read method run on a separate thread, then append a jTextField that resides on the main thread. I have the class on the second thread communicate to main class through an interface.

    First question: is this the right way to do this?

    Second question: if so, Im having some problems implementing this. below is the relevant code i have so far:

    Main thread:
    Java Code:
    public class WebAgentView extends FrameView implements MyInterface {
    
    ...
    
     private void ReadFileActionPerformed(java.awt.event.ActionEvent evt) {                                         
          if (filename == ""){
                System.out.print("no file selected");
                 jTextArea1.setText("");
                 jTextArea1.setText("You must select a file  to read.");
            }
            else{
                   Thread t = new Thread(new ReadMe());
                   t.start();
            }              
        }        
    ...
        public void AppendJText(String getIt){
             jTextArea1.append(getIt + "\n");
        }
    MyInterface interface code:

    Java Code:
    interface MyInterface {
        public void SetJ(String string);
        public void AppendJText(String string);
        public String getFileName();
    }
    class that will run on the secondary thread:
    Java Code:
    public class ReadMe implements Runnable{
        MyInterface callingFrame;
    
        public void ReadMetest(MyInterface ins){
              callingFrame = ins;
              callingFrame.SetJ("");
              
                try {
                    BufferedReader in = new BufferedReader(new FileReader(callingFrame.getFileName()));
                    String s = in.readLine();
                    while (s != null) {
                       callingFrame.AppendJText(s);
                       s = in.readLine();
                        i++;
                    }
                }
                catch (Exception exc) {
    		return;
                }
        }
    
    
        public void run() {
             (new Thread(new ReadMe())).start();
    
    
        }
    
    
    
    
    }
    When I run this, though, it does not seem to work. No errors are produced but nothing happens. I'm not sure if I'm doing the threading wrong, the interfacing wrong or both. Any help would be greatly appreciated.

    Thanks
    jason

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    catch (Exception exc) {
        return;
    }
    Never NEVER do this. Your code is throwing an exception but you have no idea as you do nothing with it. At the very least make a call to printStackTrace to get some idea of what exception is being throw and where.

  3. #3
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    yeah,
    I deleted what I had there for the post. what i did have was:
    System.out.print("error is : "+ exc.getMessage());

    But even this doesnt provide any error

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You, my friend, have created an infinite loop :D
    When the ReadFileActionPerformed() method is called, you create a thread, pass in a new ReadMe object, and call start(). start() starts a new thread and calls run(). run() in turns, creates a thread and passes in a new ReadMe object, calls start(). And the cycle begins again, forever.

    The code in the constructor of "ReadMe" should be inside "run()" ;)

  5. #5
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    thanks for your reply. I tried moving everything from the constructor to the run, but i get this error:

    Exception in thread "Thread-4" java.lang.NullPointerException
    at webagent.ReadMe.run(ReadMe.java:27)
    at java.lang.Thread.run(Thread.java:662)

    the changes I made to the ReadMe class are as follows:

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    public class ReadMe implements Runnable{
        MyInterface callingFrame;
        MyInterface ins;
    
        public void ReadMetest(){
        }
    
    
        public void run() {
    
                   callingFrame = ins;
                callingFrame.SetJ("");
    
                try {
                    BufferedReader in = new BufferedReader(new FileReader(callingFrame.getFileName()));
                    String s = in.readLine();
                    while (s != null) {
                       callingFrame.AppendJText(s);
                        s = in.readLine();
                        i++;
                    }
                }
                catch (Exception exc) {
                    System.out.print("error is : "+ exc.getMessage());
    		return;
                }
        }
    }
    Does it have something to do with how i'm using the interface?

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    No you just need to set your constructor to accept an object of type MyInterface so you initialize "ins".

  7. #7
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    I'm still kind of new to java. how would i do this? i've tried changing the constructor to:

    [code]
    public void ReadMetest(MyInterface ins){
    callingFrame = ins;

    }

    [\code]

    but i still get the same exception

  8. #8
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    never mind, i figured it out.

    so, when i created the thread, in the main class, i needed to do this:
    Thread t = new Thread(new ReadMe(this));
    t.start();
    then, in the readMe class, I had to do as you said.

    thanks for your help
    jason

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. Read a file and converting this file into a string
    By kostinio in forum New To Java
    Replies: 7
    Last Post: 12-26-2009, 03:54 PM
  2. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •