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.
Poindexter
source share