Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-27-2009, 12:35 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-28-2009, 08:18 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-30-2009, 07:53 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-30-2009, 04:48 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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 )?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-30-2009, 05:02 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Default
<duplicated>
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully

Last edited by sukatoa; 06-30-2009 at 05:36 PM. Reason: replied twice
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-01-2009, 05:19 AM
Senior Member
 
Join Date: Nov 2008
Posts: 275
Rep Power: 2
neilcoffey is on a distinguished road
Default
This sounds like the underlying thing you want to do is set a lower timeout on the socket, then?
__________________
Neil Coffey
Javamex - Java tutorials and performance info
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-01-2009, 07:48 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-17-2009, 07:41 AM
Mrs. Deswal's Avatar
Member
 
Join Date: Apr 2009
Location: Gurgaon,India
Posts: 24
Rep Power: 0
Mrs. Deswal is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 09:26 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org