Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-26-2008, 10:41 AM
Member
 
Join Date: May 2008
Posts: 26
Master Zero is on a distinguished road
[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.

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 10:44 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-26-2008, 10:53 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-26-2008, 11:06 AM
Member
 
Join Date: May 2008
Posts: 26
Master Zero is on a distinguished road
I am not sure how to go about you solution; but yeah, I wish to call the method below on its own thread.

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 11:09 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-26-2008, 11:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
public class ThreadTest implements Runnable{ Thread processThread; public void run() { while(true) { // Do the process } } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-26-2008, 06:17 PM
Member
 
Join Date: May 2008
Posts: 26
Master Zero is on a distinguished road
Thanks a million!
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-27-2008, 05:26 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
No problem my friend. Nice to hear that I'm help to solve your question.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error! "filename" is not abstract and does not override abstract method... hasani6leap New To Java 6 10-27-2008 02:25 AM
[SOLVED] Exception in thread "main" java.util.NoSuchElementException thevoice New To Java 5 05-14-2008 03:43 PM
"Jumble" or "Scramble" Program Shadow22202 Java Applets 8 04-30-2008 05:42 AM
I am confused about is the "add" method... mathias New To Java 1 08-01-2007 07:29 AM
Hwlp with "Open", "Save", "Save as..." trill New To Java 1 07-31-2007 09:53 AM


All times are GMT +3. The time now is 08:14 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org