Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

closing thread and its childs

 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guten tag...
i a have a server application which will spawn childs each time when client request a connection. the problem is, how can i stop all the childs process, immediately, in case I want to shutdown my server.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If with "shutdown" you mean "exit JVM", look into Thread.setDaemon(). If every child thread is marked as a daemon thread, the JVM will exit even if the thread is still running.
Does this help?
Otherwise, you'll have to check Thread.interrupted() inside your child thread's inner loops. You can interrupt threads blocked waiting on sockets by (yuck) closing the socket from another thread; ie. you could as part of the shutdown procedure go through the list of client threads and close their sockets. Until the Java 1.4 java.nio.* packages come along this is the only way to do it I'm afraid.
- Peter
 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
If with "shutdown" you mean "exit JVM", look into Thread.setDaemon(). If every child thread is marked as a daemon thread, the JVM will exit even if the thread is still running.
Does this help?
Otherwise, you'll have to check Thread.interrupted() inside your child thread's inner loops. You can interrupt threads blocked waiting on sockets by (yuck) closing the socket from another thread; ie. you could as part of the shutdown procedure go through the list of client threads and close their sockets. Until the Java 1.4 java.nio.* packages come along this is the only way to do it I'm afraid.
- Peter

i also have a thought of using interrupt. the problem is, i'm not really sure how. can u or anyone give an example on how to using it...
thanks....
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just off the top of my head so don't blame me...

- Peter
 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've already tried that code..
and it's works...
thanks...
but, i have a new problem...
u see..
my the interrupt is checked in a loop...
that mean, any process in that loop, have to be finish first, before the checking process can be continue...
so,....
how can i just interrput that thread, without waiting the loop to get finish....
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct. There is no way around this short of using the deprecated stop() method.
Tricks of the trade: you can force a network connection to close by calling the close() method from another thread. Also, it may be worth getting acquainted with the java.nio.* classes in JDK 1.4 (currently in beta).
- Peter
 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my code. as u can see, i'm using System.exit(-1) on the child thread. it seem to work, but i don't think that will be a good way to solve my problem. i've attached the completed code of mine. perhaps, u can check it ad give me some advices.... thanks...
my SockServer code
package cbusengine;
import java.io.*;
import java.net.*;
public class SockServer
{
public static void main(String args[]) throws IOException
{
int serverPort = 5317;
ServerSocket serverSocket = null;
boolean listening = true;
SockServerThread serverThread = null;

try
{
System.out.println("Starting CBusEngine's Socket Server.....");
System.out.print("Establishing server socket at port " + serverPort + ": ");
serverSocket = new ServerSocket(serverPort);
System.out.println("ok");
}
catch(IOException ioe)
{
System.out.println("Error!");
System.out.println("Could not listen on port: " + serverPort);
System.out.println("Server terminated!...");
System.exit(1);
}
try
{
while(listening)
{
serverThread = new SockServerThread(serverSocket.accept());
serverThread.start();
}
}
catch(IOException ioe)
{
System.out.println("Accepting client connection failed");
System.out.println("Server terminated!...");
System.exit(1);
}
try
{
System.out.print("Shutting server down: ");
serverSocket.close();
System.out.println("ok");
}
catch(IOException ioe)
{
System.out.println("Error!");
System.out.println("Server terminated!...");
System.exit(1);
}
}
}

my SockServerThread code
package cbusengine;
import java.io.*;
import java.net.*;
import cbusengine.cbus.*;

public class SockServerThread implements Runnable
{
private Socket clientSocket = null;
private PrintWriter os = null;
private BufferedReader is = null;
private SockServerProtocol Protocol = null;
private CGateCommand CGate = null;
Thread t;
public SockServerThread(Socket socket)
{
try
{
clientSocket = socket;
CGate = new CGateCommand("clippci", 20023, 20024, true, false, false);
Protocol = new SockServerProtocol(CGate);
t = new Thread(this, "ServerThread");
}
catch(CGateException e)
{}
}
public void start()
{
t.start();
}
public void run()
{
System.out.println("Accepting client connection");
try
{
System.out.print("Establishing input stream from client: ");
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("ok");
}
catch(IOException ioe)
{
System.out.println("Error!");
System.out.println("Cannot get input stream from client");
System.out.println("Server terminated!...");
System.exit(1);
}
try
{
System.out.print("Establishing output stream to client: ");
os = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("ok");
}
catch(IOException ioe)
{
System.out.println("Error!");
System.out.println("Cannot get output stream to client");
System.out.println("Server terminated!...");
System.exit(1);
}
try
{
String input;
while((input = is.readLine()) != null)
{
if(input.toLowerCase().equals("quit"))
{
os.println("closing connection.....");
break;
}
else if (input.toLowerCase().equals("shutdown"))
{
os.println("shutting down CBusEngine's Socket Server");
System.exit(1);
}
else
os.println(Protocol.processInput(input));
}
}
catch(IOException ioe)
{
System.out.println("Error reading/writing to client");
System.out.println("Server terminated!...");
System.exit(1);
}
catch(CGateException cge)
{
os.println("CGate error");
}
try
{
System.out.print("Closing client connection: ");
is.close();
os.close();
clientSocket.close();
CGate.CGateClose();
System.out.println("ok");
}
catch(IOException ioe)
{
System.out.println("Error!");
System.out.println("Server terminated!...");
System.exit(1);
}
}
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic