How to have 2 topics talk to each other?

I am currently creating an IRC bot in Java (I know there are frameworks), and I'm trying to connect it to multiple servers. The problem I encountered is not part of the connection, I just run my Connect class in x threads. Each thread connects the bot to the specified server / port. Now my problem is that when certain text is displayed by the user, the bot should report a channel that says: "you typed this command" (for example). Now I want the bot to report ALL servers, saying "you typed this command." This is just an example (which is why it does not make much sense).

Connect f = new Connect(irc.freenode.net, 6667); Thread ft = new Thread(f); ft.start(); Connect q = new Connect(irc.quakenet.org, 6667); Thread qt = new Thread(q); qt.start(); 

Now, having the above code example, I would like one thread to talk to another when entering certain text. Something like:

 if (lineReader.substring(lineReader.indexOf(":"), lineReader.length()).equals("hello")) { message both servers "Hello World!" } 

If anyone could help, I would really appreciate it. Thanks!

+7
java multithreading
source share
6 answers

You must give each thread an Incoming Message Queue that other threads can enter asynchronously; java.util.concurrent.ConcurrentLinkedQueue is likely to be a good class for this.

Then you need one instance of the MessageSender class that contains links to all your threads. If a thread wants to send a message to all other threads, it will call send (msg) in this global MessageSender object, and it, in turn, will iterate over all threads and push the message to the appropriate queues (skipping the sender thread).

threads themselves can check their turn from time to time (depending on any other logic that they can execute) and delete messages after they are processed.

+1
source share

I think a simple approach would be an observer pattern where each thread knows about all the other threads

+3
source share

The simplest solution is to pass a shared object to both threads. In this case, it can only be a String . For example:

 String messagePasser; Connect f = new Connect(irc.freenode.net, 6667, messagePasser); Thread ft = new Thread(f); ft.start(); Connect q = new Connect(irc.quakenet.org, 6667, messagePasser); Thread qt = new Thread(q); qt.start(); 

Then your other example:

 if (lineReader.substring(lineReader.indexOf(":"), lineReader.length()).equals("hello")){ messagePasser = "Hello, World!"; } 

Each thread must then constantly check the messagePasser to see when it changes, and then output the message. This is obviously a very simple suggestion and completely bypasses many synchronization and thread safety issues.

EDIT: in light of learning that String passed by value (learn something new every day ...), you just need to create a simple messagePasser object that encapsulates String and any other data you would like.

0
source share

MessagePasser solution will not work because string objects in java are passed by value, not by reference.

0
source share

From your description, I believe that each of the threads expects messages from another user in the chat. Waiting means that it calls some network method, which is returned only when a message arrives. As soon as this message arrives, the stream (optionally) generates a response, places it, and then continues to wait. Why do you have multiple threads, right?

This means, however, that threads can only send something after they receive something. Because only then did they wake up from their state of expectation. This means that it does not work to โ€œbroadcastโ€ the command response to all threads. Instead, the receiving stream should send it to all servers (while other streams are still waiting to be read from these servers).

In other words: break down the strict distribution of servers into threads. Let any stream be sent to any server.

0
source share

Add each thread to the collection, say a list, and stay tuned. Once an update is available, submit it for each thread. (Sounds like an observer ...)!

0
source share

All Articles