Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

terminating a thread

Hi, I have a server that spawns a thread for each incoming socket connection and I want to terminate the thread when the connection is removed since I no longer need the thread and want to free some cpu usage.   The Java api has destroy() method but it does not do the clean up.   Can someone tell me a good way of doing this?   Thanks.
0
skyblue01
Asked:
skyblue01
  • 4
  • 2
1 Solution
 
CEHJCommented:
If the thread has run, used its own resources, is not holding references to other resources and has no references to it, it will be garbage collected at some point, so you don't need to do anything
0
 
objectsCommented:
A thread terminates when it run() method finishes, so you need to implement a method to cause this to happen.
0
 
objectsCommented:
Something like:

public void stop()
{
   ServerSocket server = Server;
   Server = null;
   server.close();
}


public void run()
{
    while (Server!=null)
    {
       ...
    }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
skyblue01Author Commented:
objects, from what you have above, you close the ServerSocket.   When the connection is removed, I do:

Socket s = sc.socket(); // sc is ServerSocket
s.close();

Can it be either way or do you have to close the ServerSocket instead of Socket?  
0
 
objectsCommented:
You want to close the server socket as you no longer want to accept connections (if my understanding is correct).
0
 
skyblue01Author Commented:
Oh, the ServerSocket is used to accept all incoming connections, and I just want to terminate a thread that is spawned for one incoming connection.   Since I still want to be accepting additional socket conenctions... so I guess I want to close the Socket in that case.   Anyways, I'll do what you suggested.   Thanks!
0
 
objectsCommented:
Yes just close the socket then :-)

http://www.objects.com.au/staff/mick
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now