This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Apr 22, 2007 9:18 PM by EJP RSS

how to handle thread handling during exception

807606 Newbie
Currently Being Moderated
Iam using a server socket,If a client socket is accepted means iam going to invoke n number of child threads.If in case some number of threads are died means i have to restart the dead thread.I need to use the housekeeping thread which is going to invoke the deaded child thread.anyone give me the idea to do this process.... can anyone help this issue its urgent
  • 1. Re: how to handle thread handling during exception
    807606 Newbie
    Currently Being Moderated
    Do you need a thread pool?
  • 2. Re: how to handle thread handling during exception
    807606 Newbie
    Currently Being Moderated
    iam already using the thread pool,use of that pool is to retrieve the data from the queue and display the data.While my parent thread is to write the data from the client socket to queue.If my parent thread is executing normally means it will create the thread pool and handle the data.If my parent thread crashes means my child thread pool does not crashes.i want to knw how to pass the exception from the parent thread to child thread
  • 3. Re: how to handle thread handling during exception
    EJP Guru
    Currently Being Moderated
    What is this all about? If a thread dies you can't restart it. But why would it die? don't let it - catch Exception and Error at the bottom of its run() method.

    if it dies because of an IOException on the socket it means there is no more client so there is nothing for the thread to do but close the socket, abort all the transactions, forget the client's existence, and disappear.
  • 4. Re: how to handle thread handling during exception
    807606 Newbie
    Currently Being Moderated
    You are correct,I got the socket exception but still my parent thread and client thread is running,i want to stop the thread because its still in running..can u tell me about that....
  • 5. Re: how to handle thread handling during exception
    EJP Guru
    Currently Being Moderated
    If the thread gets a SocketException, just close the socket(s) affected and return all the way to the run() method and once more to get out of it, or throw some kind of RuntimeException which is caught by the run() method and treated as a reason to return, or ... plenty of possible solutions. The essential thing is that eventually you have to return out of the run() method to stop the thread.
  • 6. Re: how to handle thread handling during exception
    807606 Newbie
    Currently Being Moderated
    can u give the sample code for that or give any reference site plss...
  • 7. Re: how to handle thread handling during exception
    EJP Guru
    Currently Being Moderated
    return;
  • 8. Re: how to handle thread handling during exception
    807606 Newbie
    Currently Being Moderated
    class server
    {
    Thread t=new Thread(new parentThread(ServerSocket.accept()));
    }

    class parentThread
    {Thread t;
    parentThread(Socket s)
    {
               //Socket,Streams are created here
               t=new Thread(this);
               t.start();
    }

    public void run()
    {
    try{
    invoke();
    process();}

    catch(Exception e){};
    }

    void invoke() throws Exception
    {
    invoke n Number of child threads and all of them waiting in condition variable
    Thread[] t=new Thread(new ChildThread(OutputStream));
    }

    void process() throws Exception
    {
    get the data from the socket using InputStream and add it to Queue then it signals to the waiting child thread


    }
    }


    class ChildThread
    {
    ChildThread(OutputStream os)
    {
    Output stream created here
    }
    public void run()
    {
    get the data from the queue and wait on the condition
    }

    }

    Where i have to use return.
    I want to knw if my parent thread got exception in socket means how i have to stop the parentthread.In case my child thread got any exception how i have to close the child thread and start the same child thread again. Is it possible to get the exception of child thread in the parent thread........ can u tell me plssssss
  • 9. Re: how to handle thread handling during exception
    EJP Guru
    Currently Being Moderated
    You seem very confused about threading. There are far too many Thread objects in this code. You just need this:
    class Parent implements Runnable
    {
    }
    
    class Child implements Runnable
    {
    }
    so that starting a new Parent thread is just new Thread(new Parent(...)).start(), and starting a new Child thread is just new Thread(new Child(...)).start().

    But there is no such thing as restarting a thread. The way you have your code presently, if anything throws an exception it will propagate all the way to the run() methods, where you are presently ignoring it instead of logging, which is bad practice, and the thread will then stop. Which was what you asked. The part I don't understand is this business about restarting. If you encounter a recoverable exception you should catch it at the place where you know how to recover. If an exception gets all the way to the run() method you are in deep doodoo and the last thing you should be doing is retrying. What you should be doing in this circumstance, depending on what the exception actually is, is either logging the exception and forgetting about the thread, or else debugging your program so it doesn't happen.

    And please use code tags next time. The code you presented above just isn't readable.
  • 10. Re: how to handle thread handling during exception
    807606 Newbie
    Currently Being Moderated
    iam not understand about your solution... Iam clearly explaining what iam doing with the thread and what problem i have facing...


    When server socket is established,client socket is passed to the parent thread.. In this thread run method,Iam invoke the n number of child threads all are waiting in condition variable,then parent thread reads the data from the socket thro InputStream and write it into linkedlist.After writing the data,signal to child thread.One of the child thread waiting on the condition variable is invoked access the linked list and retrieve the data.

    the problem is,When a client connection fails,at that time parent thread doesnt read any data it will show exception so i need to stop my parent thread and child thread.How to solve this problem?

    Next one is,My parent thread working normally but if any of the child thread dies means i need to tell to parent thread ,it will invoke the new child thread.how can i achieve this?

    Iam using return in run() of parent thread ... Even my parent thread still running,iam checked with that by isAlive() method.can u tell me how to check whether tht thread died or not?
  • 11. Re: how to handle thread handling during exception
    EJP Guru
    Currently Being Moderated
    When server socket is established,client socket is
    passed to the parent thread.
    No it isn't, it is accepted by the parent thread.
    In this thread run method,Iam invoke the n number of child threads all
    are waiting in condition variable,then parent thread
    reads the data from the socket thro InputStream and
    write it into linkedlist.
    Bad idea. Each accepted socket should have its own thread and should do its own I/O. This is how everybody does it. Otherwise every client has to wait for the parent thread to get around to it, which may easily be never.
    the problem is,When a client connection fails,at that
    time parent thread doesnt read any data it will show
    exception so i need to stop my parent thread and
    child thread.
    No you don't, you just need to close the socket concerned (which you would do on getting any kind of IOException on it at all other than a raad timeout).
    How to solve this problem?
    What problem? Why do you think you need to stop alll these threads? I don't understand why you would ever stop the parent thread. Your child threads aren't client threads, they are 'worker' threads that just sit there wating for request data to process. In which case why do they need terminating?
    Next one is,My parent thread working normally but if
    any of the child thread dies means i need to tell to
    parent thread ,it will invoke the new child
    thread.how can i achieve this?
    Why? If you have a thread per accepted socket like everybody else, this problem goes away. Just make sure the child thread closes the socket on the way out, and try to make it more bulletproof while you're at it. And threads don't just 'die', they throw Exceptions and Errors for reasons that you can (a) catch and (b) mostly fix.

    If what you're really implementing is a pool of worker threads as suggested above in reply #1, see the package java.util.concurrent.
    Iam using return in run() of parent thread ... Even
    my parent thread still running,iam checked with that
    by isAlive() method.can u tell me how to check
    whether tht thread died or not?
    If it really had executed the return from run(), it would not be alive, and isAlive() will return false. If isAlive() has returned true, your assumption is incorrect, and it hasn't returned from run() at all. There is no other possibility.

    Can I strongly suggest you read the Sun Sockets tutorial before you dig this hole any deeper.
  • 12. Re: how to handle thread handling during exception
    807606 Newbie
    Currently Being Moderated
    Thanks yaar.... can u give any code or tell me source code site.....
  • 13. Re: how to handle thread handling during exception
    807606 Newbie
    Currently Being Moderated
    worker threads are still alive condition even when the parent thread died,is it ok worker thread stil in alive condtion,whether it occupy any memory.....
    If in case worker threads are died,i need to start the new worker threads how can i do that..?suggest me any thread
  • 14. Re: how to handle thread handling during exception
    EJP Guru
    Currently Being Moderated
    You haven't answered any of the questions above and you obviously haven't looked at the Sun Java Sockets tutorial either. At this point I suggest you need to discuss this assignment with a colleagure or your management. It doesn't make any sense to me.
1 2 Previous Next