This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of React Cookbook: Recipes for Mastering the React Framework and have David Griffiths & Dawn Griffiths on-line!
See this thread for details.
Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic