Re: How to distinguish between two running threads
Angus <nospam@gmail.com> wrote:
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) {}
}
}