Given a really oversimplified example:
Class A {
B b = new B();
}
Class B {
//unicorns and what-not
//Something happens and I want to let A know
//Yet I don't want to return and exit
}
Is there any way that B can communicate with A without Sockets?
By communicate, I mean B sending values to A without A invoking a method on B.
EDIT: Thank you for your responses. This following is my follow up question:
If I had the following, and method signal() is called simultaneously by 2 instances of B, this will cause a conflict of every B calling action twice. What can I do to solve it?
//Modified from Jon Skeet
public class A {
private B b[];
public A() {
//for loop
b[i] = new B(this);
}
public void signal() {
//for loop
b[i].action();
}
}
public class B {
A creator;
public B(A creator) {
this.creator = creator;
}
public void action() {
//stuff
}
public void main(String[] args) {
while(true)
if(something==true) {
creator.signal();
}
}
}
Give them both access to the same
Queue. One puts elements onto it, the other pulls elements from it. If they’re in separate threads, one of theBlockingQueueimplementations should do the trick.