0

Basically I have a ServerSocket listener , on new incoming connection the program executes a thread to serve it , after the thread finishes , the program doesn't continue this is the listener

client = listenSocket.accept();
        new HandleConnection(client);//HandleConnections extends thread and start 
                                         //method is called in the constructor                    
        counter++;
        System.out.println("Number of clients served : " + counter);

this is the thread

public HandleConnection(Socket client) {
        this.client = client;
        this.start();
}
public void run() {

        try {
            in = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
            handler();
            System.out.println("Ending Thread !");

        } catch (IOException e) {
            //System.out.println("socket closed");
            e.printStackTrace();
        }

    }

the message "Ending Thread !" is executed normally , but the counter++ and the following println statement are never executed

Exorcismus
  • 2,243
  • 1
  • 35
  • 68
  • 1
    where do you execute start method of thread? – Jayyrus Dec 17 '13 at 14:41
  • 2
    You haven't posted enough code to answer, but I can tell you something else: your `run` method is one big resource leak. – Marko Topolnik Dec 17 '13 at 14:43
  • 1
    @MarkoTopolnik I assume you are talking about missing `finally` clause to close the streams? – Gray Dec 17 '13 at 14:45
  • 1
    @gray Yes. There are altogether three resources he needs to close. – Marko Topolnik Dec 17 '13 at 14:46
  • OP, I wonder if you have checked for the `"Number of clients served"` message *before* the `"Ending Thread !"` message---because that is where it is most likely to occur in the console output. – Marko Topolnik Dec 17 '13 at 14:47
  • As I mentioned above System.out.println("Ending Thread !"); is executed , doesn't this mean the thread should finish by now ? also , I do close streams and socket in the handler function... – Exorcismus Dec 17 '13 at 14:50

2 Answers2

2

the message "Ending Thread !" is executed normally , but the counter++ and the following println statement are never executed

So if new HandleConnection(client); actually starts a new thread (which you should not do in a constructor, see below), then the counter++ should immediately be executed and the "Number of clients... message printed. Any chance the message is appearing above the "Ending Thread!" message in your logs? Typically it takes some time to start the actual thread so the caller will continue to execute before the run() method is entered.

Other comments about your code:

  • As @MarkoTopolnik mentions, you need to close the input and output streams in your run() method. finally clauses are a required pattern there.
  • You should never call Thread.start() in an object constructor because of Thread race condition issues around object construction. See: Why not to start a thread in the constructor? How to terminate?
  • Instead of extending Thread you should consider implementing Runnable and doing something like:

    new Thread(new HandleConnection(client)).start();
    
  • Event better than managing the threads yourself would be to use an ExecutorService thread-pool for your client handlers. See this tutorial.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
0

The typical way to do this is to perform something like this:

    ServerSocket listener = new ServerSocket(3030); //Create a new socket to listen on

    try
    {
        //While we are running, if a client connects
        //accept the connect and increment the client ID
        while (true)
        {
            new udSocketServer(listener.accept()).start();
        }
    }
    finally //Gracefully close
    {
        listener.close(); //Close socket object
    }

You could then call the shared variable 'counter' in the thread constructor. If you need more than this, let me know and I will edit. But in reality you need a little more code for us to answer.

Brant Unger
  • 403
  • 2
  • 11