Discussions

EJB design: unable to use thread.interrupt

  1. unable to use thread.interrupt (4 messages)

    Hi,
       I have a serversocket running as a thread ( i.e. socket is listening continuously in the run method of that thread).
    I am not able to invoke thread.interrupt on that thread. Can somebody suggest a way to stop that thread?


    Thanks
    Puru

    Threaded Messages (4)

  2. ServerSocket[ Go to top ]

    Close the server socket and fall out of the run method.

    class Server implements Runnable
    {
      ServerSocket socket;

      void run()
      {
        try
        {
          while(true)
          {
            handle(socket.accept());
          }
        }
        catch(SocketException e)

      }

    }
  3. ServerSocket[ Go to top ]

    Missed a bit

    Close the server socket and fall out of the run method.

    class Server implements Runnable
    {
      ServerSocket socket;

      void run()
      {
        try
        {
          while(true)
          {
            handle(socket.accept());
          }
        }
        catch(SocketException e)

      }

      void shutdown()
      {
        sock.close();
      }
    }
  4. ServerSocket[ Go to top ]

    thanks. I am attaching the skeleton code which I used... please let me know if I am doing any error

    /////////////////////////////

    class server implements Runnable {

    /**
    * Main processing method for the server object
    */
    public void run() {

    try {
    while (true) {
    //open socket and keep listening and process the received messages for ever


    }
    } catch (InterruptedException e) {

    // somebody interrupted the thread.. stop the socket and end
                            socket.close;
    e.printStackTrace();
    return;// ends this thread
    }
    }


    /**
    * The main program for the server class
    *
    *@param args The command line arguments
    */
    public static void main(String[] args) {

    ThreadGroup group = new ThreadGroup(
    "server group");

    server p = new server();
    serverp1 = new server();
    Thread worker = new Thread(group, p);
    Thread worker1 = new Thread(group, p1);
    worker.setName("worker thread");
    worker.setPriority(1);

    worker.start();
    worker1.start();
    group.interrupt();// should shutdown the server...

    // its shutting down the server I do not use socket... from the
    definition of interrupt, it will interrupt interruptablechannel,wait,sleep etc
    somehow I am not able to get this thing to interrupt a thread in which a socket
    is waiting for connections
    }


    }


    /////////////////////////////////
  5. ServerSocket[ Go to top ]

    It will only cause this to happen if you are using an InterruptableChannel from the new nio stuff. I don't think that using the ServerSocket straight you are doing this. Try using a ServerSocketChannel instead. This can be obtained by creating a standard ServerSocket and calling getChannel. accept() on ServerSocketChannel will throw a ClosedByInterruptException - If another thread interrupts the current thread while the accept operation is in progress, thereby closing the channel and setting the current thread's interrupt status.

    Hope this works

    Stu