[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Please send email if you are having login problems - see the posts below for more info.
Hotmail and Yahoo! users - please see the Hotmail post or the Yahoo! post for information on lost emails.
accept() locks application  XML
Forum Index » J2EE Application Development
Author Message
McPete

Newbie

Joined: 04/25/2006 09:13:33
Messages: 8
Offline

Hey,

I'm trying to create a login server, whereby a java client connects to a remove java application. On the server side, I am trying to use the accept() method to wait for the input. However, when I run this method, it seems to cause the program to lock up (The swing "On" button remains down, and the GUI goes blank if minimized and maximized).

Do I need to create a new Thread for this method? I looked up Swing and Threads on the sun site, and it said use with extreme caution, so I'm a bit dubious...

Here's the problem code...

Thanks,

Pete.


mrider

Newbie

Joined: 10/25/2005 11:50:02
Messages: 25
Offline

Do I need to create a new Thread for this method?

Yes. When the tutorial says "use with extreme caution", they mean "make sure you do it correctly", not "stay away until you know better".

Arguably the easiest thread type is one where the thread goes off in it's own direction, runs to completion, and never talks back to the original thread - and it looks like you can do that here. It makes life so much easier if you're not synchronizing access to data, or waiting on events, or etcetera.

My suggestion: Wrap that code you've posted into a separate object that implements the Runnable interface. Then in your action listener for the button, simply create a new thread for that object, set it's priority correctly (event handlers run at too high of a priority for "normal" processing), and start it. Assume your server listener is called "MySocketListener" - you'd start it like this:

MySocketListener msl = new MySocketListener();
Thread t = new Thread( msl );
t.setPriority( Thread.NORM_PRIORITY );
t.start();

In essence what you're doing is starting a new thread with your listener, and then releasing it "into the wild".

HTH
McPete

Newbie

Joined: 04/25/2006 09:13:33
Messages: 8
Offline

Thank you very much mrider!

Got that problem sorted... Now to just master sockets and I'm there
 
Forum Index » J2EE Application Development
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2011 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle