This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping a thread  RSS feed

 
D Preethi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have some code which is calling a 3rd party API. This 3rd party API internally creates a thread and starts it. Now, in my code, I want to attach a functionality to be able to cancel the running of this 3rd party API anytime. I want to do this without stopping all the threads of the application. (so, cant use System.exit() ). And, I am not sure how to call Thread.interrupt() from my code because the thread is actually created and started in the 3rd party API. Can you suggest me some way to stop this thread at will?


Thanks in advance,
Preethi
 
Gopinath Karyadath
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

How to Stop a Thread or a Task ?
this URLl will help you - http://forward.com.au/javaProgramming/HowToStopAThread.html

Regards
Gopinath
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably you have access to the thread the API creates (which we could help with) or was that your question ?

In general this sounds like a bad idea ;-)
 
Kaustav Ganguly
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try invoking the third party API from your own Thread and then interrupt your own thread when you require to interrupt the third party thread.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can effectively look up their thread by name (use the debugger to see what they named it or not) (using some coding and ThreadGroups) (or other similar tricks if the name of there thread isn't exact) and just call interrupt on it was what I was getting at , if that was what the original question was after ? code examples exist. ;-)

Still not a good plan ;-)
 
Pramod Chris
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd agree, it's not a good idea, but if your API call does return a java.util.concurrent.Future object or if it provides a way to get one, you could wait till that thread finishes by calling it's get() method.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember each thread has its own interruption policy. you should not interrupt a thread unless you know what interruption means to that thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!