Server e thread

In my application i would create a multithread server.

When i launch the server, it listens on a port (with accept method) and i must manage it from consolle.

While it manages the connection in by client, i must can controll the server from the consolle, such as see how many connections are opened, how long time it's running, and stop it.

So, i launch the accept method in a thread.

When i would stop the server, how i can stop the accept method in the other thread?

[500 byte] By [PremierITAa] at [2007-11-27 7:52:23]
# 1

I do not quite understand your problem, but perhaps that this will help:

I am guessing that you want to stop a thread. An easy way to do this is to have a boolean variable in a thread, and have the thread check that boolean value for each loop iteration. If the boolean is false, the thread will stop.

class CustomThread extends Thread {

private boolean stopped;

public void run() {

while (!stopped) {

//code, such as accept();

}

//stopped == true

//clean up resources

}

public void end() {

stopped = true;

}

}

//external code in another class

CustomThread t = new CustomThread();

t.start(); //start the thread

//later on....

t.end();

//t will end running shortly.

RATiXa at 2007-7-12 19:33:37 > top of Java-index,Core,Core APIs...
# 2
I've applayed the same solution, but when i stop the loop (with boolean variable at false) the accept method doesn't stop. So it gives me an exception.
PremierITAa at 2007-7-12 19:33:37 > top of Java-index,Core,Core APIs...
# 3
Set a timeout on the server socket and check the boolean every time it happens.
ejpa at 2007-7-12 19:33:37 > top of Java-index,Core,Core APIs...