I'm developing a simple chat client and I would need the server to handle multiple threads (1 per connection)
At the moment I only have 1 user and 1 connection
Thread con = new Thread(new Connection());
con.start();
Connection() is responsible for listening for messages from this particular connection and broadcasting them to each client (at the moment there is only one)
I plan to create an array of Connection objects and create a thread for each but i'm not sure what i should do from here on, what does 'con' actually represent in this case?
If Connection
is a custom class containing information about a certain connection (which I assume it is), then you don't want to pass it into the Thread.
You could probably benefit from reading the Java documentation concerning Defining and Starting a Thread. What you probably want is to start a new Thread()
every time you receive a connection from a client. You can accomplish this with this snippet:
new Thread(){
public void run() {
System.out.println("blah");
}
}.start();
Whatever code you put inside the run()
function will get run inside a thread.
To answer the second part of you question, in your example, your con
object represents a single instance of a thread of execution.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments