File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Implementing Cancel Operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Implementing Cancel Operation" Watch "Implementing Cancel Operation" New topic

Implementing Cancel Operation

Bhagya Tangutur
Ranch Hand

Joined: Oct 21, 2002
Posts: 88
what is the ncie way to implement Cancel Operation.
say i issued a command based on user operation,but suddenly user wants to stop the operation.I don't like to execute single statement
after user sends operation cancel request.besides i don't like to check some condition before executing each statmet.

Sun Certified Java Programmer
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

You don't need to check a condition before EVERY statement, but the best way to implement a "cancel" functionality is to have your long-running operation check some condition periodically. Response within a half-second or so is perceived by the user as instantaneous -- and millions of statements can execute in that amount of time! Of course, most long-running operations are loops, and you'll typically check the condition each time around the loop.
Often one thread is running some operation, while another is waiting for it to complete using join() or wait(). In this case, you can interrupt the waiting thread, and it will return immediately; then you can set the condition so the task thread will stop eventually. This kind of "two-phase" cancel works nicely.
The alternative to polling a condition is to use Thread.stop() -- and really, you don't want to do that.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Implementing Cancel Operation
It's not a secret anymore!