Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi-threading - passing text to each thread in threadgroup

Status
Not open for further replies.

prasadmokashi

Programmer
Oct 31, 2003
41
US
Hi,

I am new to socket programming and threads in java. I am trying to do the following.

When the client connects, socket accepts the connection and then hands it over to thread & goes back to listening requests....
created thread keeps on waiting...
Now at any point of time, I have number of such threads waiting ( depending upon no of connections ).

Now when I receive some text from some other program, I would like to pass that text to all the threads waiting and wake them up.

I have the following code to handle the connection.

Could anybody please tell me, on receiving the message how would I be able to call send function for each thread in the threadgroup ??? OR is there any other simple way of handling this ?

protected class HandleConnection extends Thread {
private String message;

// the output stream
PrintWriter out;
public HandleConnection(ThreadGroup thg,Socket theConnection) {
super(thg,theConnection.getInetAddress().toString());
try {

PrintWriter out = new PrintWriter(theConnection.getOutputStream(), true);
} catch (IOException ioe) {
System.err.println("Couldn't get io stream on new socket connection");
}
}


public void send(String msg){
this.message = msg;
this.notify();
}

public void run() {
try {

while(true) {
synchronized (this) {
wait();

//send message
out.println(this.message);
}
}

} catch (Exception e) {}

}

}
 
Consider this code (and apply it to yours) :

Code:
public class ThreadTest {


 	public static void main(String args[]) throws Exception {
		new ThreadTest().test();
	}

	public void test() throws Exception {
		InternalThread[] threads = new InternalThread[2];
		InternalThread it = new InternalThread();
		it.start();
		threads[0] = it;
		it = new InternalThread();
		it.start();
		threads[1] = it;

		Thread.sleep(2000);

		threads[0].sendMessage("hello");
		threads[1].sendMessage("there");

		Thread.sleep(2000);

		threads[0].sendMessage("hello2");
		threads[1].sendMessage("there2");
	}

	class InternalThread extends Thread {
		String message = "none";
		public void run() {
			while (true) {
				try {
					synchronized(this) {
						wait();
					}
					System.err.println(message);
				} catch (InterruptedException ie) {
					ie.printStackTrace(System.err);
				}
			}
		}

		public void sendMessage(String message) {
			this.message = message;
			synchronized(this) {
				notify();
			}
		}
	}

}

 
Hi,

Thank you very very much.

By referring to the snippet provided by you, I could solve my problem.

Thanks again.

Regards,
Prasad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top