Solved

terminating a thread

Posted on 2003-12-11
7
350 Views
Last Modified: 2010-03-31
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
Comment
Question by:skyblue01
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9924499
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
 
LVL 92

Expert Comment

by:objects
ID: 9924552
A thread terminates when it run() method finishes, so you need to implement a method to cause this to happen.
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 9924562
Something like:

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


public void run()
{
    while (Server!=null)
    {
       ...
    }
}
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:skyblue01
ID: 9924616
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
 
LVL 92

Expert Comment

by:objects
ID: 9924630
You want to close the server socket as you no longer want to accept connections (if my understanding is correct).
0
 

Author Comment

by:skyblue01
ID: 9924651
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
 
LVL 92

Expert Comment

by:objects
ID: 9924667
Yes just close the socket then :-)

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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 45
Spring Framework HTTPSession management 1 36
Oracle SQL syntax check  without executing 6 57
java stored proc example 9 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question