简体   繁体   中英

Java communication between threads

I am writing a program to demonstrate the distance vector routing algorithm. This program creates multiple routers and their routing table. Each router is its own thread using a two-dimensional array. I need to have the threads send their tables to one another. I am not sure how to communicate between these threads. I looked into sockets but am not listening on any ports. I am not sure how to use pipes or if this is the best option or not. Any suggestions would be helpful.

If you run two different processes, you can use Sockets, JMS or files to share information.

If you run just two threads in one process, you should create some thread safe storage, say ConcurrentLinkedQueue for this. Here is more of them http://javarevisited.blogspot.com/2013/02/concurrent-collections-from-jdk-56-java-example-tutorial.html

Have each Runnable or Thread that implements your routing thread expose (say) a java.util.concurrent.TransferQueue<YourTableType> and deliver your tables to it. The routing thread can pull the tables from the transfer queue at it's leisure.

The java.util.concurrent package is extremely well documented, so it's worth having a good look around in it.

Why don't create a server/client and make the call from the client to the server and then the server send the answer to the other client ?

You'll need to create 2 files, server.java and client.java, Make the connection client/server via TCP, Messages from client to client via UDP;

If you need any help just ask, I've made a few projects like that in university.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM