Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Indipendent threads in Java

Thread view: 
BlackLight - 25 Jun 2006 20:20 GMT
Hi,
I would like to know if there's a way to make in Java two or more
indipendent threads, without freezing one of them (such as with join() and
yield() methods) on one side and without letting one of them terminate
before its natural end on the other.
In my case, I'm trying to develop a NMAP-like port scanner with GUI. Once
pressed the button "Scan" on the main frame a new thread starts to scan
all the open ports on the remote host. But if I don't specify anything the
thread stops in a while, often without finding any open port, while, if I
use the join() method on the main thread or something like:

while (t.isAlive())  Thread.yield();

the main frame freezes until the other process is ended.
Hope you can help me.
Thanks.

Signature

questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it

Eric Sosman - 25 Jun 2006 21:05 GMT
> Hi,
> I would like to know if there's a way to make in Java two or more
[quoted text clipped - 10 lines]
>
> the main frame freezes until the other process is ended.

    The two threads execute independently (note spelling)
until you do something to force them to interact.  By calling
join(), for instance, you explicitly tell one thread to stop
executing until the other finishes.  The threads' executions
can also become entangled if they synchronize on the same
object, or if one calls wait() to stall itself until the other
calls notify() or notifyAll().

    The "main frame freezes" when the event dispatching thread
is busy doing something other than dispatch events.  While the
EDT is sitting in join() or in a while loop or something, it is
not dispatching the events that drive the GUI.  If you don't
want the GUI to freeze, don't tie up the EDT for long intervals.

    I think your real problem isn't about stalling one thread
while the other runs, but about how the "servant" thread reports
its results back to the "master."  What is supposed to happen
when the scanner thread finishes its scan?

Signature

Eric Sosman
esosman@acm-dot-org.invalid

BlackLight - 26 Jun 2006 08:06 GMT
> What is supposed to happen when the scanner thread finishes its scan?

Once the scanner thread is over a new frame is opened, with the results of
the scan.

Anyway, if I don't call the join() method on the main thread or if I don't
make a while loop until the scanner thread is over the scanning process
stops immediately, often without reporting any result.

The actual code is something like this:

// The main class:
public void actionPerformed(ActionEvent e)  {
 if (e.getActionCommand() == "Scan" )  {
   Thread t = new Scan();
   t.start();

   try  {
     t.join();
   }

   catch (InterruptedException e)  {}

   // Or: while (t.isAlive())  Thread.yield();

// And this is the method run() in the "Scan" class:

public void run()  {
 curPos=0;
 int ports[] = new int[PORT_NUM];

 for (int i=0; i<PORT_NUM; i++)  {
   try  {
     Socket s = new Socket(host_name,i);

     if (!s.isClosed())  {
       ports[curPos]=i;
       curPos++;
     }
   }

   catch (IOException e)  {}
   ........

And then it prints the results of the scan (the open ports now stored in
the array "ports") in a JTable or something like that. But using join() or
yield() causes the main frame to "freeze" until the scanner thread is
over, and I just wanted to know if there's a way to keep it alive while
the scan is in progress...thanks for the help.

Signature

questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it

Matt Humphrey - 26 Jun 2006 13:01 GMT
>> What is supposed to happen when the scanner thread finishes its scan?
>
[quoted text clipped - 16 lines]
>      t.join();
>    }

^^^ You are launching a separate thread from the EDT and then promptly
making the EDT wait until that thread is finished--that's the same as not
using a thread at all.  By making the EDT wait the UI will appear to freeze
and results will not display until the entire thread finishes.

>    catch (InterruptedException e)  {}
>
[quoted text clipped - 24 lines]
> over, and I just wanted to know if there's a way to keep it alive while
> the scan is in progress...thanks for the help.

First, drop the join.  Second, when you have results in your run method, do
the following to make the EDT display them.  UI objects (Swing) should only
be manipulated from the EDT--except for one or two they're not threadsafe.
This technique allows non-EDT threads to request UI changes in a thread-safe
way.

SwingUtilities.invokeLater (new Runnable () {
 public void run () {
   // Whatever code you had above to display your results should go here.
   // It will be executed in the EDT thread which will preserve thread
safety.
   // You can use a similar technique to display partial results and status
   // information--you don't have to wait for your Scan thread to finish.
 }
});

Cheers,
Matt Humphrey matth@ivizNOSPAM.com  http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.