Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:31:29+00:00 2026-06-07T22:31:29+00:00

Basically, I have a long task that consists of bunch of sequence sub-tasks as

  • 0

Basically, I have a long task that consists of bunch of sequence sub-tasks as follows:

class Task implements Runnable
{
    private Foo foo;

    public Task(Foo foo)
    {
        this.foo = foo;
    }

    @Override
    public void run()
    {
        doTask1(foo);
        doTask2(foo);
        doTask3(foo);
        doTask4(foo);
        // ...
        doTaskN(foo);
    }
}

What I need is to run only one instance of the Task, and if one Task begins, the other Tasks (if any) should terminate immediately.

I used a single thread executor:

Executor executor = Executors.newSingleThreadExecutor();

and I run the task as:

executor.execute(new Task(foo));

This guarantees that only one task executes at a time, but unfortunately it does not terminate the previous tasks.


However, I decide to use a boolean flag in between each 2 sub-tasks as follows:

class BooleanHolder
{
    boolean terminate = false;
}

class Task implements Runnable
{
    private Foo foo;
    private BooleanHolder bh;

    public Task(Foo foo, BooleanHolder bh)
    {
        this.foo = foo;
        this.bh = bh;
    }

    @Override
    public void run()
    {
        bh.terminate = false;
        doTask1(foo);
        if(bh.terminate) return;
        doTask2(foo);
        if(bh.terminate) return;
        doTask3(foo);
        if(bh.terminate) return;
        doTask4(foo);
        // ...
        if(bh.terminate) return;
        doTaskN(foo);
    }
}

and use it like this:

BooleanHolder bh = new BooleanHolder();

// ...

bh.terminate = true;
executor.execute(new Task(foo, bh));

However, this seems to be inefficient solution and not thread safe. Do you suggest a better solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:31:30+00:00Added an answer on June 7, 2026 at 10:31 pm

    You should use the ExecutorService interface, instead of Executor, like this:

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<?> future = executor.submit(new Task(foo));
    

    You can then interrupt the running task in order to execute another task through the cancel method:

    future.cancel(true);
    

    As @RPT said however, you should structure your tasks in such a way to respond to interrupts, otherwise the interrupt signal will not have any effect. If your task has no operation which throws an InterruptedException, you should manually check the interrupt flag from time to time (similar to what you already did):

    @Override
    public void run()
    {
        ....
        if (Thread.interrupted()) {
            // Release resources and end task
        }
        ....
    }
    

    By the way, this solves your problem, but does not actually terminate the thread inside ExecutorService.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have the following class: .sf-sub-indicator { background: url(/abcprod/images/arrows-ffffff.png) no-repeat -10px -100px; }
I have a long running task that I need to implement on a webpage.
Well basically I have this script that takes a long time to execute and
I basically have a program that filters records from one excel file to another
I basically have 7 select statements that I need to have the results output
Basically I have a lot of tasks (in batches of about 1000) and execution
I have a long running php script which is basically an infinite loop listening
I have something that looks like the following document structure: public class Document {
Need some advice, I'm after a decent process/task manager for Ubuntu. Basically I have
Basically I have some very long variables and need only the first few characters.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.