How to distinguish between two running threads

A

Angus

Hello

I have an applet and I want to create two threads - one to handle inbound
network io and the other outbound.

So I do this:
public class MyExample extends Applet implements ActionListener,
ItemListener, Runnable

Thread nwioIn; // Inbound network handler thread
Thread nwioOut; // Outbound network handler thread

and this:

nwioIn = new Thread(this);
nwioIn.start();
mwioOut = new Thread(this);
nwioOut.start();

Then I have a Run function

But I can have only one Run function? So in my Run function I kick off a
Inbound network io thread and then a outbound network io thread. do I just
use eg a static bool eg first and if first = true then do In then when false
do outbound? Would that work?

Angus
 
T

Thomas Fritsch

Angus said:
Hello

I have an applet and I want to create two threads - one to handle inbound
network io and the other outbound.

So I do this:
public class MyExample extends Applet implements ActionListener,
ItemListener, Runnable

Thread nwioIn; // Inbound network handler thread
Thread nwioOut; // Outbound network handler thread

and this:

nwioIn = new Thread(this);
nwioIn.start();
mwioOut = new Thread(this);
nwioOut.start();

Then I have a Run function

But I can have only one Run function? So in my Run function I kick off a
Inbound network io thread and then a outbound network io thread. do I just
use eg a static bool eg first and if first = true then do In then when false
do outbound? Would that work?
May be, but it would be a pain, as you already noticed.

You need 2 different Runnable objects, each with its own run() method.
From your description the 2 Runnables have to do quite different jobs.
I would therefore suggest to create 2 new classes for them, for example like
follows:

public class InboundHandler implements Runnable {
... // some private variables
InboundHandler(???) {
... // initialize private variables
}
public void run() {
...
}
}

public class OutboundHandler implements Runnable {
... // some private variables
OutboundHandler(???) {
... // initialize private variables
}
public void run() {
...
}
}

Your applet won't need to have a run() method (and hence doesn't have to
implement Runnable) anymore.
You can start your 2 threads like this:
nwioIn = new Thread(new InboundHandler(???));
nwioIn.start();
mwioOut = new Thread(new OutboundHandler(???));
nwioOut.start();

You will probably have to write some stuff for the constructors (at "???"
above), so that you can pass parameters from your applet to your In- and
OutboundHandler.
 
W

wesley.hall

Angus said:
Hello

I have an applet and I want to create two threads - one to handle inbound
network io and the other outbound.

So I do this:
public class MyExample extends Applet implements ActionListener,
ItemListener, Runnable

Thread nwioIn; // Inbound network handler thread
Thread nwioOut; // Outbound network handler thread

and this:

nwioIn = new Thread(this);
nwioIn.start();
mwioOut = new Thread(this);
nwioOut.start();

Then I have a Run function

But I can have only one Run function? So in my Run function I kick off a
Inbound network io thread and then a outbound network io thread. do I just
use eg a static bool eg first and if first = true then do In then when false
do outbound? Would that work?

Angus


Hi Angus,

Here is what you should do...

public class IOThingy
{
public static void main(String[] args)
{
new Thread(new OutputHandler()).start();
new Thread(new InputHandler()).start();
}

private class OutputHandler implements Runnable
{
public void run()
{
//Do output work
}
}

private class InputHandler implements Runnable
{
public void run()
{
//Do input work
}
}
}


Using inner classes like this allows you to have two different run
method implementations. You will need to adapt this example for your
applet.
 
W

wesley.hall

Angus said:
Hello

I have an applet and I want to create two threads - one to handle inbound
network io and the other outbound.

So I do this:
public class MyExample extends Applet implements ActionListener,
ItemListener, Runnable

Thread nwioIn; // Inbound network handler thread
Thread nwioOut; // Outbound network handler thread

and this:

nwioIn = new Thread(this);
nwioIn.start();
mwioOut = new Thread(this);
nwioOut.start();

Then I have a Run function

But I can have only one Run function? So in my Run function I kick off a
Inbound network io thread and then a outbound network io thread. do I just
use eg a static bool eg first and if first = true then do In then when false
do outbound? Would that work?

Angus


Hi Angus,

Here is what you should do...

public class IOThingy
{
public static void main(String[] args)
{
new Thread(new OutputHandler()).start();
new Thread(new InputHandler()).start();
}

private class OutputHandler implements Runnable
{
public void run()
{
//Do output work
}
}

private class InputHandler implements Runnable
{
public void run()
{
//Do input work
}
}
}


Using inner classes like this allows you to have two different run
method implementations. You will need to adapt this example for your
applet.
 
A

Andreas Leitgeb

Angus said:
I have an applet and I want to create two threads - one to handle inbound
network io and the other outbound.

So I do this:
public class MyExample extends Applet implements ActionListener,
ItemListener, Runnable

I've been told once, that making some central (in the
sense that it does more jobs than just running a thread)
class itself Runnable is bad style, for two reasons:

It means, your class declares itself being runnable even to the
outside world, (there is no way to hide a class's inheritence
tree, except by hiding the class itself). So anyone who sees
an instance of your class could theoretically start a thread
with your class's run-method.
Second, you can only define *one* run-behaviour per class.
(run() as implemented for Runnable takes no arguments!)
(You'd have to parametrize the instances before start()ing
the threads with them)

Alternatively, you just define any arbitrary Methods in your
class: doInbound() and doOutbound, and for the threads you
use internal anonymous classes:

(see at bottom of this post for a "working" example)
Thread nwioIn; // Inbound network handler thread
Thread nwioOut; // Outbound network handler thread
nwioIn = new Thread(this);
nwioIn = new Thread (
new Runnable() { public void run() { doInbound(); } }
)
nwioIn.start();
mwioOut = new Thread(this);
nwioOut = new Thread (
new Runnable() { public void run() { doOutbound(); } }
)
nwioOut.start();

I believe, while a bit more complicated, this would be
considered proper style.
do I just use eg a static bool eg first and if first = true
then do In then when false do outbound? Would that work?

It wouldn't have to be a static bool. It could just as well be
non-static. Since in your example all threads start the same
exact object, they will see the same field, whether static or
not.
Either way, this will put your programs logic to high
risk!!!, because whichway ever you put it, it's possible
that depending on race-conditions among these newly started
threads, both may end up doing the same:

bool doOut; // at class level: a field
void setupThreads(...) {
nIn=new Thread(this); nOut=new Thread(this);
doOut=false; nIn.start();
doOut=true; nOut.start();
}
It's obvious that if the first created thread takes just a bit
too long before querying doOut, it will find doOut already set
to true.


There actually *are* some hackish ways to let MyExample be Runnable
and still let the threads find their right job, but to really make
them safe, they become actually more complicated than the anonymous
classes, offered above.
Really.

Full example for anonymous classes:
(foo() and bar() are static to make them easily callable from main,
Their being static is not relevant otherwise.)

class MyExample {
static void foo() { System.out.println("foo"); }
static void bar() { System.out.println("bar"); }
public static void main(String[] args) {
Thread tFoo=new Thread(
new Runnable() { public void run() { foo(); } }
);
tFoo.start();
Thread tBar=new Thread(
new Runnable() { public void run() { bar(); } }
);
tBar.start();
try { Thread.sleep(5); } catch (Throwable t) {}
}
}
 
H

henry

I think, this is the best solution. But you could do something like
that to just to simply distinguish between Threads, too ...


public class ThreadTest implements Runnable{
Thread t1;
Thread t2;

void go(){
t1 = new Thread(this);
t2 = new Thread(this);
t1.start();
t2.start();
}

public void run() {
if(Thread.currentThread()==t1){
// ...
}else{
//...
}
}
}

But as I said before,I think Wesley`s way is the better one
Angus said:
Hello

I have an applet and I want to create two threads - one to handle inbound
network io and the other outbound.

So I do this:
public class MyExample extends Applet implements ActionListener,
ItemListener, Runnable

Thread nwioIn; // Inbound network handler thread
Thread nwioOut; // Outbound network handler thread

and this:

nwioIn = new Thread(this);
nwioIn.start();
mwioOut = new Thread(this);
nwioOut.start();

Then I have a Run function

But I can have only one Run function? So in my Run function I kick off a
Inbound network io thread and then a outbound network io thread. do I just
use eg a static bool eg first and if first = true then do In then when false
do outbound? Would that work?

Angus


Hi Angus,

Here is what you should do...

public class IOThingy
{
public static void main(String[] args)
{
new Thread(new OutputHandler()).start();
new Thread(new InputHandler()).start();
}

private class OutputHandler implements Runnable
{
public void run()
{
//Do output work
}
}

private class InputHandler implements Runnable
{
public void run()
{
//Do input work
}
}
}


Using inner classes like this allows you to have two different run
method implementations. You will need to adapt this example for your
applet.
 
S

Simon Brooke

Angus said:
Hello

I have an applet and I want to create two threads - one to handle inbound
network io and the other outbound.

So I do this:
public class MyExample extends Applet implements ActionListener,
ItemListener, Runnable

Thread nwioIn; // Inbound network handler thread
Thread nwioOut; // Outbound network handler thread

and this:

nwioIn = new Thread(this);
nwioIn.start();
mwioOut = new Thread(this);
nwioOut.start();

Then I have a Run function

But I can have only one Run function? So in my Run function I kick off a
Inbound network io thread and then a outbound network io thread. do I
just use eg a static bool eg first and if first = true then do In then
when false
do outbound? Would that work?

You can subclass Thread and give each subclass it's own run() method. Or,
perhaps better, you can create two classes each of which implement
Runnable, and pass each to a separate thread.
 
L

Lew

Andreas said:
I've been told once, that making some central (in the
sense that it does more jobs than just running a thread)
class itself Runnable is bad style, for two reasons:

Anytime you have a class with a tag that determines the behavior, as in the
boolean deciding whether run() does input or output, one should suspect an
inheritance model is better.

See Joshua Bloch's _Effective Java_, item 20, "Replace unions with class
hierarchies".

- Lew
 
A

Andreas Leitgeb

Lew said:
Anytime you have a class with a tag that determines the behavior, as in the
boolean deciding whether run() does input or output, one should suspect an
inheritance model is better.

See Joshua Bloch's _Effective Java_, item 20, "Replace unions with class
hierarchies".

I don't think this applies to the outset of this thread.
Both, in unions and in class-hierarchies, it's all about having
separate instances (either of a union, or of different derived
classes). In the OP's problem, it's about having *one* instance
of a class, doing two different things in it's implementation of
Runnable.run().
 
L

Lew

Andreas said:
I don't think this applies to the outset of this thread.
Both, in unions and in class-hierarchies, it's all about having
separate instances (either of a union, or of different derived
classes). In the OP's problem, it's about having *one* instance
of a class, doing two different things in it's implementation of
Runnable.run().

Depending on a tag to determine which version of run() to run. If there were
two implementors of Runnable, one for each version, then in lieu of the tag
the object's class would determine which run() to run, polymorphically.
Instead of passing in a tag, pass in the correct Runnable.

- Lew
 

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,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top