File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Need to stop the thread ,but not using stop()method after the server.accept() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Need to stop the thread ,but not using stop()method after the server.accept() method" Watch "Need to stop the thread ,but not using stop()method after the server.accept() method" New topic
Author

Need to stop the thread ,but not using stop()method after the server.accept() method

rosan samuel
Ranch Hand

Joined: Nov 17, 2010
Posts: 37
Hi Friends,

my problem is a server socket is created and waiting to accept the client connections indefinite time of thread loop. Assume inside the while loop, the a server socket waits for connection. but in my case there is socket connection from client not happened.i need to stop the thread ,when the application server calls resource adapter destroy() method.This method intentionally close the Connection pool connections as well as The running thread and has to close the ServerSocket. but problem is how could we perform the ServerSocket.close and after the accept method. kindly share your thoughts.

public void run() {
acceptConnections();
}

protected void acceptConnections() {
try {
// Bind to a local port
if(server==null)
{
server = new ServerSocket(_port);
}
while(running) {

// Accept the next connection
Socket connection = server.accept();

// Check to see if maximum reached
if (getNumberOfConnections() >= MAX_CONNECTIONS) {
// Kill the connection
PrintStream pout = new PrintStream(
connection.getOutputStream());
pout.println("Too many users");
connection.close();
continue;
} else {
// Launch a new thread
new XXXClient(this, connection).start();
}
}


} catch (InterruptedIOException e) {
logger.log(Level.FINE, "This can be considered a normal exit", e);
} catch (IOException e) {
logger.log(Level.SEVERE, "Unable to accept connections", e);
}
}


Java Bee
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

You could try making a public method that sets running to false which will break that loop and let the thread exit cleanly. The thread stop() method is rarely a good idea, it's much better to let the thread finish processing. Inside the loop you could also add a conditional test for every iteration, that will allow you cleanly break all connections and close your streams so that you don't run into problems with hung resources.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to stop the thread ,but not using stop()method after the server.accept() method