I'm trying to write an instant messaging program in Java. What I have coded so far, goes like this: Each client connects to a thread created by the server, to which he/she sends the message. My doubt is, how can I kill all the threads that have been created by the main program when the administrator presses de 'q' in the keyboard. I made a class for a thread whose only function is to read the 'q' from the keyboard, but, how can I notify to the main program that it must kill all the threads?

This is the Server (it is not complete):

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listening = true;
		new StdinReaderThread();

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not create socket");
            System.exit(-1);
        }

        while (listening){
        	new ServerThread(serverSocket.accept()).start();
		}
        serverSocket.close();
    }
}

This is the thread which reads 'q' from keyboard:

public class StdinReaderThread implements Runnable{
		BufferedReader stdin;

		public StdinReaderThread(){
			Thread subpr = new Thread(this,"x");
			stdin = new BufferedReader( new InputStreamReader(System.in));
			subpr.start();
		}
		public void run(){
			while (true){
			   try{
			      String stdinline = stdin.readLine();
			      if( stdinline.startsWith("q")){
				    System.out.println("end");
			       }
				  else if( stdinline.startsWith("m"))
					System.out.println("send message");
				  else if( stdinline.startsWith("l"))
					System.out.println("send login");
				  else if( stdinline.startsWith("u"))
					System.out.println("list users");
			    } catch (Exception e){}
		    }
        }
}

Recommended Answers

All 4 Replies

with

join();

if you keep all the threads you created in an array,vector...
then you can call join() on all threads to kill them all...Of course, this will cause all threads to be done.

If you mean stopping server imediately, then just close the server...
server.close();

with

join();

if you keep all the threads you created in an array,vector...
then you can call join() on all threads to kill them all...Of course, this will cause all threads to be done.

If you mean stopping server imediately, then just close the server...
server.close();

Thanks gangsta1903,
But how can I notify the main program that it's child thread StdInReaderThread has just detected a 'q' from the keyboard ? And, what happens if the main program is blocked on a socket and StdInReaderThread detects a 'q'?

Thanks gangsta1903,
But how can I notify the main program that it's child thread StdInReaderThread has just detected a 'q' from the keyboard ? And, what happens if the main program is blocked on a socket and StdInReaderThread detects a 'q'?

Your approach is right. On server side, before starting the server-client mechanism, first start your stdInReaderThread. Its up to you what to do when you get "q". You can just close the server. But,for example if its a file sharing program, then you should probably wait last file transfer before closing. But in your program its just messaging, so just closing the server will serve the purpose...
What I mean, there wont be a blocking... Client sends message and it comes instantly, its not a long lasting process. Of course when you close the server, messaging if any will be interrupted.

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listening = true;
        // start the stdinReader first
	new StdinReaderThread().start();

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not create socket");
            System.exit(-1);
        }

        while (listening){
        	new ServerThread(serverSocket.accept()).start();
		}
        serverSocket.close();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.