Indipendent threads in Java

B

BlackLight

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.
 
E

Eric Sosman

BlackLight said:
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.

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?
 
B

BlackLight

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.
 
M

Matt Humphrey

BlackLight said:
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();
}

^^^ 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) {}

// 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.

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 (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top