Results 1 to 8 of 8
  1. #1
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default Forcing a thread to stop

    Good day,

    I have a problem about trying to force the specific thread to stop while it is still waiting for a specific method to return.

    By convention, having a flag inside the run method and by using it to return the said method is effective, but how about "still waiting to return the invoked method inside run method"?

    sample:

    public void run(){
    String src = instance.getData();
    }

    where instance.getData() method will return after several minutes of waiting. I want to force that thread to stop ( since having a flag is not effective in that scenario ). Any other alternatives on how to reproduce this one?

    Anybody?
    I'm looking forward to your replies :)
    freedom exists in the world of ideas

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    ok, the interrupt method solves the problem(depends on how will it be implemented),

    now, i want only the getData method to be stopped( while waiting for the returned value ) and not the succeeding instructions.

    The alternative idea i could do is to throw that instruction in another thread and the succeeding instructions is also in another thread(waiting for a new value returned from getData through synchronized instance communication). But, i doubt to proceed hoping there's more convenient way to re-implement it. Any ideas?
    freedom exists in the world of ideas

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    What is getData() doing? Interrupting the thread only creates an exception if the thread is in a blocked state, such as a socket read. If getData() is blocking, the interrupting the thread will lead to an interruped exception. You can put a try/catch around the getData() call, or you could modify getData() to catch the exception.

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    it attempts to crawl the specific url and return its source(html)... some websites accept http connection but doesn't return any, that's why it will wait for how many minutes,hours...

    So you mean, if an interrupt method invoked, the InterruptedException will be thrown from current instruction( getData )?
    freedom exists in the world of ideas

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    <duplicated>
    Last edited by sukatoa; 06-30-2009 at 04:36 PM. Reason: replied twice
    freedom exists in the world of ideas

  6. #6
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    This sounds like the underlying thing you want to do is set a lower timeout on the socket, then?

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Yes, in fact, it happens on the socket command that is waiting for a response. Neil's reply contains a good solution, if you have control over the socket buried inside getData(). You will still have to handle whatever happens when the socket times out (possibly an Exception).

    If you can't modify the socket, I would suggest putting the getData() call in its own thread. I *think* you could use an inner class that implements Runnable, so that the class could easily pass the received data back to the thread starter as well as notify the thread starter that the task was complete. Have the thread starter wait for a specified time and then interrupt the thread if it has not returned. Of course, the thread starter itself will be blocked, and you will have synchronization issues to address.

  8. #8
    Mrs. Deswal's Avatar
    Mrs. Deswal is offline Member
    Join Date
    Apr 2009
    Location
    Gurgaon,India
    Posts
    24
    Rep Power
    0

    Default

    Hi,


    I was having same problem.
    you can make this thread as daemon thread. Then the control will be in parent thread. So if you want to stop this thread after a certain interval of time then you can stop this thread from the thread where this thread is invoked.
    Sharing knowledge means gaining more knowledge.:)

Similar Threads

  1. [SOLVED] How to stop a thread
    By AlejandroPe in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 03:05 PM
  2. How to stop thread from being jumping off the code without executing it.....
    By chiragkini in forum Threads and Synchronization
    Replies: 6
    Last Post: 01-22-2009, 03:38 AM
  3. Can you stop a gif? xd
    By Exhonour in forum New To Java
    Replies: 0
    Last Post: 01-16-2009, 08:44 PM
  4. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 AM
  5. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •