Results 1 to 10 of 10
  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation Client freezes when method is called

    I have a problem.
    Basically, I have created a client programmed using MVC.
    The client view freezes when recieveMessage() is called. But when it isn't called, the gui doesn't freeze.
    Either SwingUtilities and EventQueue class doesn't work.

    What should I alter to solve this thread.
    Should I use SwingWorker?

    Java Code:
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    import java.net.InetAddress;
    import java.net.Socket;
    
    import javax.swing.JTextArea;
    import java.awt.EventQueue;
    
    public class Model {
    
        private Socket socket;
        private String computerName;
        private String serverIPAdd;
        private int portNumber;
        private ObjectInputStream input;
        private ObjectOutputStream output;
        private JTextArea messageField;
        private String message = "";
    
        public Model(String computerName, String serverIPAdd, int portNumber, JTextArea messageField) {
            this.computerName = computerName;
            this.serverIPAdd = serverIPAdd;
            this.portNumber = portNumber;
            this.messageField = messageField;
        }
    
        public void runClient() {
            try {
                connect();
                recieveMessage();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    
        public void close() {
            try {
                socket.close();
                input.close();
                output.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
    
        }
    
        private void connect() throws IOException {
            socket = new Socket(InetAddress.getByName(serverIPAdd), portNumber);
            input = new ObjectInputStream(socket.getInputStream());
            output = new ObjectOutputStream(socket.getOutputStream());
            output.flush();
            display("Connected to Server.");
        }
    
        private void recieveMessage() throws IOException {
            try {            
                do {
                    message = (String)input.readObject();
                    display(message);
                } while(!message.contains("Terminate"));
            } catch(ClassNotFoundException e) {
                e.printStackTrace();
            }
            
        }
    
        protected void sendMessage(final String message) {
            try {
                String data = computerName + ": " + message;
                output.writeObject(data);
                output.flush();
                display(data);
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    
        private void display(final String message) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    messageField.append(message + "\n");
                }
            });
        }    
    }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    client view freezes when recieveMessage() i
    Can the read method block waiting for input? Use available() to test if there is input.

    Do you call the blocking method on its own thread?
    If you use Swing's GUI thread, then the GUI will freeze if the read method blocks.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by chyrl View Post
    The client view freezes when recieveMessage() is called. But when it isn't called, the gui doesn't freeze.
    Either SwingUtilities and EventQueue class doesn't work.
    You have a Swing concurrency issue.

    What should I alter to solve this thread.
    Should I use SwingWorker?
    Yes.

  4. #4
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by Norm View Post
    Can the read method block waiting for input? Use available() to test if there is input.
    The model does accept the input.readObject().
    I haven't tried yet the blocking method I think.
    But meaning the throws is making the interruption to completion?

    Quote Originally Posted by Norm View Post
    Do you call the blocking method on its own thread?
    If you use Swing's GUI thread, then the GUI will freeze if the read method blocks.
    What solution can be done?
    What pseudocode, class, method am I going to utilize?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  5. #5
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by Fubarable View Post
    You have a Swing concurrency issue.
    In what part of the code?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    There have been many posts on this topic on this forum. I'm sure there is one that had the same problem as you're having. Use Search here to find the other threads.

    Basically you need to have a thread that does the I/O between server and client that is separate from Swing's GUI (EDT) thread. Swing's thread MUST not be used for reading data from other sites. Start a thread to do that.

  7. #7
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by Norm View Post
    There have been many posts on this topic on this forum. I'm sure there is one that had the same problem as you're having. Use Search here to find the other threads.

    Basically you need to have a thread that does the I/O between server and client that is separate from Swing's GUI (EDT) thread. Swing's thread MUST not be used for reading data from other sites. Start a thread to do that.
    So your saying that I should convert the recieveMessage() into Runnable?
    And pull-out the display() out of the do-while()?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I haven't looked at your code closely enough to make coding change recommendations.
    There is a bit more to Creating a separate thread than that.

  9. #9
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by Fubarable View Post
    Yes.
    Am I going to SwingWorker the recieveMessage()?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  10. #10
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Alas.
    I have understood now the concept you given me.
    This was the last adjustment to the recieveMessage()

    Java Code:
    private void recieveMessage() {
            thread = new Thread() {
                public void run() {
                    do {
                        try {
                            message = (String) input.readObject();                   
                            display(message);
                        } catch(IOException e) {
                            e.printStackTrace();
                        } catch(ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    } while(!message.contains("Terminate"));
                }
            };
            thread.start();        
        }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Similar Threads

  1. Why the paint() method is called two times ?
    By supremo in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 07:21 PM
  2. Replies: 10
    Last Post: 02-28-2010, 02:52 PM
  3. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 11:19 PM
  4. [SOLVED] Method from one thread called on another thread
    By Ypsilon IV in forum Threads and Synchronization
    Replies: 7
    Last Post: 04-24-2009, 03:07 PM
  5. Replies: 2
    Last Post: 10-31-2007, 04:55 PM

Posting Permissions

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