Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to interrupt /stop a thread which is running a long query  RSS feed

 
Rnk msra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

can any one suggest me some thing....I am running a thread using start () method which in turn calls the run() method of runnable object . Inside run() I am executing a very long running sql query....Now suddenly I need to stop the query /stop the thread...how can I do this from out side the thread that is executing SQL...

Thanks,
Rinko
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends exactly on what you are doing.

If you are processing in logical chunks, the best solution would be to check the state of a variable, and quit processing if the variable is set. For example:


If you aren't processing in chunks, or if most of the work is done in the Database, and so your thread is more or less just waiting for the database to complete, then you can close the connection to the DB when you want to stop.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...start () method which in turn calls the run() method of runnable object


To be precise,invoking the start method on a Thread object doesn't call the run() method on the runnable object.
Calling start() would lead to creation of a thread (usually a kernel space thread (NATIVE thread) for modern OSes with support for kernel space threads..for old JVMs implemented for special OSes with no support for kernel level multi-threading,a user space thread in JVM would be created (GREEN threads).)

The time of invocation of the run() method on the associated runnable object would be strictly determined by the thread scheduling algorithm. Again ,depending on whether the JVM implements a user space co-operative multithreading or delegates the issue of multithreading to the OS kernel,the scheduler could either be part of the JVM or reside on the kernel.

 
Gabriel Vince
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you are true about the main problem - your thread is waiting for DB to reply and you cannot do much about it.
I am thinking about what you want to achieve.. you want to cancel your request after a time or on demand?

If a timeout is enough for you, you may call setQueryTimeout(seconds) on your statement.

If you want to stop the query on demand (a cancel button?), I see no other choice as to abadon your request, which still holds DBresources, DB under load, but your application will respond)

Anyway for it I would use wait/notify locks


what you have are two thread instead of one (wait method should not be called in the AWT or web worker thread for performance reasons) and you have still allocated resources for DB (and your DB under load).

maybe there is a more DB native way to tell DB to stop the request, but I don't think anordinary JDBC will handle this.

good luck
Gabriel

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!