Communication amongst Classes
Hi,
I do hope this is the right spot to "release" my question...here goes.
In my current project, I have several Classes that need to "talk" to each other - they exchange a string, nothing more. Due to the nature and ease of mainenance, each Class is located in a seperate file. How do I go about that?
More tech info: there is one central Class (an Applet), with "sattelite" Classes (declared and instanced) within that one Class. The "sattelites need to communicate, for that a third set runs in a Thread (impl. Runnable) - this one "picks" up the messages and "delivers them to the destination Class. Well, that's the idea anyway...
Ideas?
Thanks in advande.
Master Thor.
[732 byte] By [
T01deva] at [2007-11-27 2:58:24]

# 1
Classes don't talk to each other, thread do.If you can post a little, formatted code, perhaps I can see what you are doing.
# 2
Hi, Thanks for a quick reply.
Well, I dont have anything yet, I'm looking for a way to let separate components (Threds - as you have suggested wisely it seems) to communicate. Threads have this piped communication, it seems.
Actually, it's quite simple. I have a JInternalframe handling login. As one clicks (after filling out) the OK, the thing does the check in de DB. It schould "signal" to the main Jframe that the login failed or not, and what reason (bad password, blocked, unknown user ...)
Other modules should use the same path, in order to streamline communications in the application...
As such, there is no code yet, sorry...but I can venture in any direction. But Threads do seem the best option...
Master Thor
# 3
the way i do it is to use
PipedInputStream pin = new PipedInputStream();
PipedOutputStream pout = new PipedOutputStream(pin);
MyThreadClass mtc = new MyThreadClass(pin);
mtc.start();
the communication can only be 1-way.
the above means that the Thread that created "mtc"
can send objects to it like:
List obj = new ArrayList();
ObjectOutputStream oos = new ObjectOutputStream(pout);
pout.writeObject(obj);
// beware of sending containers with non-Serializable objects.
and then in MyThreadClass, you get the PipedInputStream
in the constructor. then, to read what the thread that started you
writes:
ObjectInputStream ois = new ObjectInputStream(pin);
List obj = (List) ois.readObject();
to make commincation bi-directional, the thread needs to start:
PipedInputStream pin1 = new PipedInputStream();
PipedOutputStream pout1 = new PipedOutputStream(pin1);
PipedInputStream pin2 = new PipedInputStream();
PipedOutputStream pout2 = new PipedOutputStream(pin2);
MyThreadClass mtc = new MyThread(pin1, pout2);
// pin1 receives from the mtc
// pou2 write to the mtc
and, reading from PipedInputStream blocks unless you
look at java.nio.*
or, what i do is that in a blocking pipe, there is always a 4-byte
header. so:
while(...) {
...
if(pin.available() > 4) {
String s = (String) ois.readObject();
}
}
be forewarned that this is my first post.
# 4
Hi,Thanks for the extrensice reply...one question: how do I do that in an App with multiple Threads? How doe they know "who to call"?How about Interfaces? Can that be done?Master Thor
# 5
> Thanks for the extrensice reply...one question: how
> do I do that in an App with multiple Threads? How doe
> they know "who to call"?
not sure what that means but if you are planning on
having lots of threads then stick the open pipes into
a container. and then Iterate over that container to check
the status of each Pipe. remembering that a read pipe with
nothing to say has no more than its 4 byte header.
> How about Interfaces? Can that be done?
threaded classes can be defined as either:
public class MyClass extends Thread { ....}
MyClass obj = new MyClass(pin, pout);
obj.start();
or
[code]
public class MyClass implements Runnable { ... }
Thread t = new Thread(new MyClass(pin, pout));
t.start();
in both cases, you must have the public method:
public void run() { ..... }
i like using the Runnable interface for no other reason than
Java allows only single inheritence (and i am sure thats a
stupid reason).
-=-
# 6
So the "trick" is to keep a sort of "phonebook" on what Thread uses what pipe...did'nt see that one comming - cool. As far as Runnable is concerned, it seems to be the cleanest and more flexible way. it's my preferred mode too...
Ok, so I'll give the "phonebook" a whack!
Thank you!
Master Thor
# 7
> So the "trick" is to keep a sort of "phonebook" on
> what Thread uses what pipe...
the analogy to "phonebook" sounds like
you want a HashMap.
and be aware hashing on open pipes might
not do what you expects. so maybe think of
hashing some stateless object on to
the stateful open pipes/sockets/etc.