views:

66

answers:

3

is there any way two threads within the same process can communicate without knowing anything about each other's interface ?

basically, one thread is a STOMP server, the other is a client. they're supposed to communicate in a direct manner (not via a socket) and it should be independent of the implementation so i can't assume either of the threads is waiting for messages on some common message queue. what i'm looking for is some kind of a built-in mechanism in java that allows threads within the same process to communicate.

is there such a mechanism ? and if not, is there any other way to approach this ?

A: 

Well, you can have a third thread to act as a message board. But then you'd have to hope that the two threads can agree on a protocol before hand. It would also be rather slow.

piggles
It wouldn't necessarily have to be a third thread, just an object that both threads have access to, e.g. a message/event queue.
ZoogieZork
well the "hope" part is problematic.basically, one thread is a STOMP server, the other is a client.they're supposed to communicate in a direct manner (not via a socket) without "knowing" each other's implementation.so what i'm looking for is some kind of a built-in mechanism in java that allows threads within the same process to communicate.
Yes. this is true. You would need to make sure to check for and protect against concurrent modification
piggles
no I don't think you can do what you want to. You need to have at least some minimal previous agreements.
piggles
+4  A: 

You can use a concurrent message queue where threads can post and receive messages. Instead of knowing the other's thread interface, now each thread must be able to create own messages and understand the messages of other threads. By using a distinct interface for these messages, this is rather easy. And as a bonus, there is a wide range of queues for concurrent access available, so you can pick the queue that fits most to your scenario.

PartlyCloudy
A: 

Can you provide more details/examples? What do you mean by "communicate" exactly?

There are a few ways I can think of for doing this, shared (global) state, PipedInputStream/PipedOutputStream etc. But the details will depend on what you're trying to do.

Jack Leow