Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cross thread communication java

So I am new to Java, I have done a bit of c programming. I am trying to make a virtual network of nodes, each node would need to be a thread. The nodes are only allowed to talk to their neighbor nodes. there will be a master node that can talk to any node but the nodes would have to talk to each other to get back to the master node. the master nodes neighbors can talk to the master node.

I was originally going to keep an array list of the nodes, but then I realized all the nodes needed to be there own thread.

My question is how do I pass information back in forward between threads in Java. I need to be able to have the master node give all the regular nodes position information. and I need the regular nodes to be able to pass messages to their neighbor regular nodes.?

here are my git repos if you would like to look at the code I got going now.

https://github.com/fieldju/cs372_project

in C I made a program that used pipes for the children to talk to each other and a server connected the clients, but in this problem the nodes to to have p2p communication as most of them can not directly communicate to the master node / server


Just an Update for anyone who looks at this and wants to see the results. I got the nodes up and running and communicating you can check out the code at

https://github.com/fieldju/cs372_project

I am still working on the distance vector stuff and a few other things but by the end of next week the entire thing should be done.

like image 586
fieldju Avatar asked May 25 '11 18:05

fieldju


2 Answers

I was originally going to keep an array list of the nodes, but then I realized all the nodes needed to be there own thread.

You can keep an array of threads, it would still maintain a thread-per-node with the same logic structure.

how do I pass information back in forward between threads in Java.

If threads reside in the same process then definitely sockets are an overkill. I would use one or several ConcurrentLinkedQueue instances to push/pop messages.

One or several really depends on the kind of communication that you are implementing. Maybe one ConcurrentLinkedQueue per node, so nodes push messages to queues and every node knows where from to pop the message.

Few hints for implementation

Wrap up all the logic to en-route messages in a class - let's call this class VirtualNetwork. VirtualNetwork deals with all the instances of ConcurrentLinkedQueue and offers an API of methods to all threads for sending/receiving messages. Make one instance of the class VirtualNetwork accessible to all nodes - by passing a reference to it on the Thread constructor.

This is an sketch of how your class NodeThread would be. Notice that the classes VirtualNetwork and Message are classes that you have to implement your self.

class NodeThread extends Thread {

    private int nodeId;
    private VirtualNetwork network;

    public NodeThread(int nodeId,VirtualNetwork network) {
        this.network = network;
        this.nodeId = nodeId;
    }
    public void run() {
        /* when you have to send */
        int nodeReceptor = this.nodeId -1; /* neighbor in the array of threads */
        Message m = new Message(this.nodeId,nodeReceptor);
        m.setContent(10);
        network.send(m);

        /* when you have to receive */
        Message m = network.receive(this.nodeId);
        /* it's your decision to implement this in a blocking way or not */
    }
} 
like image 107
Manuel Salvadores Avatar answered Sep 23 '22 14:09

Manuel Salvadores


An easy (if not optimal) way to start is to create a BlockingQueue for each pair of threads that need to pass values in one direction (if it's bidirectional you need twice as many.)

like image 30
finnw Avatar answered Sep 24 '22 14:09

finnw