Accessing a Thread by name?

Hello everyone,I have a question about thread programming. I have several threads running and the are all named. I'm wondering if there is a way from my main code to access a specific thread by name?for example, my main program wants to call a method in "ThreadB"?

I'm programming a chat type application and I need to be able to add a client to a host thread ...... here's an example

packetType = recvData.getInt();

if(packetType == JOIN_HOST)

{

// This is the name of the thread the client wants to

// connect to.

String hostName = recvData.getString();

// Here is where I need to access a specific thread based on the host

// the client wants to connect to.Any Ideas?

}

I know this question seems brief but if anyone can help I would be very thankful....

Thanks,

Mike

[1069 byte] By [Tigerfanga] at [2007-10-3 3:58:43]
# 1
Thread.getName() returns the name of the thread.Just keep track of the threads you create and search through the list. Such as:MyThread mt1 = new MyThread(...)MyThread mt2 = new MyThread(...)Putting these in a vector is easier.
cooper6a at 2007-7-14 21:57:17 > top of Java-index,Core,Core APIs...
# 2
Keeping them in a hashmap would be easiest.But I wonder if the OP is asking the right question. Calling a method on a thread object is no different to calling a method on any other object.
davidholmesa at 2007-7-14 21:57:17 > top of Java-index,Core,Core APIs...
# 3

david:not sure if I am,let me explain a little more.I am writing a chat program and I have a couple different classes. I have a separate host program that a host user logs in with. The server will spawn a new thread for each host that is logged in. These host threads are what i am talking about. A client will log in and be shown a window with a list of the online host users. I have the client selecting a host user and sending the request to the server...... here is where my problem is. I need to, based on the name the client send, connect the client with the desired host. So, If I make a method in the host class is it accessable outside the thread?

If not, how can I add a client object to a running host thread?

Sorry if I'm not explaining this correctly.

Mike

Tigerfanga at 2007-7-14 21:57:17 > top of Java-index,Core,Core APIs...
# 4

I would expect a chat program to be based around sockets/streams. When the user selects Chat-A a rmeote message is sent to the server that says "connect me with Chat-A". The server would then lookup chat-A and return the appropriate port# to the client. The client thread will then send/receive messages via that port. On the server side each chat would have a server thread listening to their dedicated port and managing the connections (of the clients) that turn up on that port. This would all be remote communication across VM's.

davidholmesa at 2007-7-14 21:57:17 > top of Java-index,Core,Core APIs...
# 5

David: So, what you're saying is that each host thread should bind it's own server socket, and then in order for a client to connect to it it will just send a join request to that socket?That does seem to make more sense then the way I'm doing things :)I don't know why but I never thought of doing that, lol.

Tigerfanga at 2007-7-14 21:57:17 > top of Java-index,Core,Core APIs...
# 6
I have another question regarding this topic. Should I treat my host threads as client programs within the server? IE when a host thread is started create a socket and connect to the bound ServerSocket? or should I just open a socket and start sending data ?
Tigerfanga at 2007-7-14 21:57:17 > top of Java-index,Core,Core APIs...
# 7

> I have another question regarding this topic. Should

> I treat my host threads as client programs within the

> server? IE when a host thread is started create a

> socket and connect to the bound ServerSocket? or

> should I just open a socket and start sending data ?

Sorry I don't understand the question. I'm not a sockets expert and don't play with them much. You're reinventing a well worn wheel here - there must be oodles of references to open source chat servers out there that you could learn from.

davidholmesa at 2007-7-14 21:57:17 > top of Java-index,Core,Core APIs...
# 8

You've got this back to front. There is no connecting to the server socket from inside the server, and no joining the socket either.

You will start one ServerSocket. Clients will then connect. You will have a server thread looping around ServerSocket.accept(). Every time you accept a socket you will start a new thread to handle it representing that client.

You need to have a good look at the Sockets tutorial for a start.

ejpa at 2007-7-14 21:57:17 > top of Java-index,Core,Core APIs...