Results 1 to 6 of 6
  1. #1
    Master Zero is offline Member
    Join Date
    May 2008
    Posts
    26
    Rep Power
    0

    Default [SOLVED] "Threading" a method?

    I am trying my hands at socket programming but I am kind of stuck. I am not sure how to implement multi-threading in java. I have a method call listen which I would like to run on a separate thread so that it will not freeze the GUI while it waits. The class is below, and any help is greatly appreciated.

    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    import java.util.Date;
    import java.awt.event.*;
    
    public class ChatServer extends JFrame implements ActionListener {
    
    	/**
    	 * @param args
    	 */
    	
    	// Variables
    	TextField msg;
    	TextArea room;
    	
    	JButton listen;
    	
    	PrintStream os;
    	DataInputStream is;
    	Socket clientSocket;
    	ServerSocket server;
    	
    	public ChatServer() {
    		server = null;
    		clientSocket = null;
    		
    		msg = new TextField();
    		room = new TextArea();
    		
    		listen = new JButton("Listen");
    		
    		this.add("South", msg);
    		this.add("Center", room);
    		this.add("North", listen);
    			listen.addActionListener(this);
    		
    		this.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				try {
    					if (server != null) {
    						server.close();
    					}
    					if (clientSocket != null) {
    						clientSocket.close();
    					}
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    					room.append(e1.getMessage());
    				}
    				
    				System.exit(0);
    			}
    		});
    	}
    	
    	public static void main(String[] args) {
    		ChatServer f = new ChatServer();
    			f.setBounds(0, 0, 250, 200);
    			f.setTitle("Chat Server");
    			f.setVisible(true);
    	}
    	
    	public boolean keyDown(Event evt, int key) {
    		if (key == 10) {
    			room.append(msg.getText() + "\n");
    			msg.setText("");
    		} return super.keyDown(evt, key);
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		if (e.toString().split(",")[1].substring(4).equals("Listen")) {
    			try {
    				server = new ServerSocket(4000);
    				
    				room.append("Server now listening for client...\n");
    				this.clientSocket = server.accept();
    				
    				room.append("Client has connected!\n");
    				os = new PrintStream(this.clientSocket.getOutputStream());
    				is = new DataInputStream(this.clientSocket.getInputStream());
    				
    				this.sendText("Welcome, current login time is :" + new Date());	
    				listen();
    			} catch (IOException e2) {
    				// TODO Auto-generated catch block
    				e2.printStackTrace();
    				room.append(e2.getMessage());
    			}
    		}
    	}
    	
    	public void sendText(String message) {
    		os.println(message);
    		os.flush();
    	}
    	
    	public void listen() {
    		try {
    			room.append(is.readLine() + "\n");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			room.append(e.getMessage());
    			System.exit(0);
    		}
    		listen();
    	}
    }
    Last edited by Master Zero; 05-26-2008 at 09:44 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You want to add another thread basically. Before that seems to me your GUI freez. May be you use some deprecated methods which is not allowed in my Java version.

  3. #3
    Master Zero is offline Member
    Join Date
    May 2008
    Posts
    26
    Rep Power
    0

    Default

    I am not sure how to go about you solution; but yeah, I wish to call the method below on its own thread.

    Java Code:
    public void listen() {
    		try {
    			room.append(is.readLine() + "\n");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			room.append(e.getMessage());
    			System.exit(0);
    		}
    		listen();
    	}
    Right now, the method is simply calling it’s self over and over.

    Thanks for the reply.
    Last edited by Master Zero; 05-26-2008 at 10:09 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I just say keyDown() and readLine() are deprecated since 1.1

    You can try something like this. In this way, not effect to GUI thread.

    Java Code:
    public class ThreadTest implements Runnable{
    
        Thread processThread;
        
        public void run() {
            while(true) {
                // Do the process
            }
        }
    
    }

  5. #5
    Master Zero is offline Member
    Join Date
    May 2008
    Posts
    26
    Rep Power
    0

    Default

    Thanks a million!

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. Replies: 6
    Last Post: 10-27-2008, 01:25 AM
  3. Replies: 5
    Last Post: 05-14-2008, 02:43 PM
  4. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 AM
  5. I am confused about is the "add" method...
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 06:29 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
  •