Results 1 to 6 of 6
- 05-26-2008, 08:41 AM #1
Member
- Join Date
- May 2008
- Posts
- 26
- Rep Power
- 0
[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 08:44 AM.
- 05-26-2008, 08:53 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-26-2008, 09:06 AM #3
Member
- Join Date
- May 2008
- Posts
- 26
- Rep Power
- 0
I am not sure how to go about you solution; but yeah, I wish to call the method below on its own thread.
Right now, the method is simply calling it’s self over and over.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(); }
Thanks for the reply.Last edited by Master Zero; 05-26-2008 at 09:09 AM.
- 05-26-2008, 09:45 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 } } }
- 05-26-2008, 04:17 PM #5
Member
- Join Date
- May 2008
- Posts
- 26
- Rep Power
- 0
Thanks a million!
- 05-27-2008, 03:26 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No problem my friend. Nice to hear that I'm help to solve your question.
Similar Threads
-
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 09:26 AM -
Error! "filename" is not abstract and does not override abstract method...
By hasani6leap in forum New To JavaReplies: 6Last Post: 10-27-2008, 12:25 AM -
[SOLVED] Exception in thread "main" java.util.NoSuchElementException
By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 01:43 PM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM -
I am confused about is the "add" method...
By mathias in forum New To JavaReplies: 1Last Post: 08-01-2007, 05:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks