0

I have a task executor which takes runnable as a task. I am starting a timer before calling runnable.run() method and stopping it when the runnable finished. I want to terminate the execution of run() method from the executor itself if the timer exceeds the time limit. I do not know what user will implement in run().

TaskExecutor.add(new Runnable () {
    @Override
    public void run() {
        System.out.println("This is test job");
    }}
);

This is how the user adds a new task. Every task runs in the same thread.

Edit
This task executor will act as a service to users. And because creating threads are expensive operation and requires native OS calls, I am trying to avoid them. Otherwise I would call Thread.interrupt() at some point. But I just want to know if there is a way to terminate the run() method from a parent object. Terminate means to stop something abruptly. As how we terminate processes in OS task manager.

How tasks are executed

while (jobQueue.isEmpty()) {
    for (Job job : jobQueue) {
       long startTime = System.currentTimeMillis();
       job.run();
       //There is a separate thread which checks
       //for timeout flags by comparing the startTime
       //with the current time. But all tasks are
       //executed in the same thread sequentially. I
       //only want to terminate single jobs that are
       //timed out.
    }
}
OmerHalit
  • 401
  • 5
  • 18
  • 4
    You can't stop arbitrary code to run. The method needs to collaborate, by reacting to interrupts and/or polling the value of some thread-safe flag. And no, never, ever use the stop() method of Thread, which is deprecated for very good reasons. – JB Nizet May 24 '17 at 06:47
  • You can also wrap your Runnable into `java.util.concurrent.FutureTask`. It has method public `boolean cancel(boolean mayInterruptIfRunning)` and arrange that `get` will return the given result on successful completion. – Jay Smith May 24 '17 at 06:53
  • Possible duplicate: https://stackoverflow.com/questions/10630737/how-to-stop-a-thread-created-by-implementing-runnable-interface – JFreeman May 24 '17 at 06:55
  • @JFreeman That question talks about stopping a thread. That is not what I intend to do. – OmerHalit May 24 '17 at 07:07
  • @JBNizet I do not have control over what user implements in the `run()` method. This executor serves as a service. – OmerHalit May 24 '17 at 07:29

2 Answers2

0

you can check condition for your timer,if timer exceeds you can interrupt your thread like-

Thread.currentThread().interrupt();

It will stop your current Thread.

gajju_15
  • 527
  • 4
  • 17
  • There is only one `thread` to execute the given tasks which implement `Runnable`. I do not want to stop the thread but a single task. – OmerHalit May 24 '17 at 07:20
  • Then you can cancel specific task, if you want to execute other tasks . if you post your code this may help to understand correctly. – gajju_15 May 24 '17 at 11:07
0

I just want to know if there is a way to terminate the run() method from a parent object.

You can't really terminate a method, and you can't really do anything from an object.

It's often convenient to say "this object does X," or "this method does Y," but objects and methods don't really do anything. When you're talking about multi-threaded code, it's important to realize that everything your program does is done by threads. Methods are just the instructions that tell threads what to do, and objects are what the threads do it to.

You can interrupt a thread, which is only a good idea if the thread is designed to gracefully handle the interrupt; and you can terminate a thread, which basically is never a good idea.

Threads should always cooperate with one another. You need to provide a means by which your program can politely ask the client-provided callback to abort its work and return early. If the client code does not respect your request (i.e., if the client code does not cooperate), that's the client programmer's fault for not obeying your guidelines.

The simplest way to do it would be to simply expose some static boolean method that the client can periodically check to see whether it's time to abort.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57