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

  • Home
  • SEARCH
  • 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 8418949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:28:16+00:00 2026-06-10T02:28:16+00:00

I have thread application which process heavy task, i would like to trigger forcibly

  • 0

I have thread application which process heavy task, i would like to trigger forcibly stop thread upon external flag information. I have tried following design,

public class HeavyTaskThread implements Runnable
{
   private boolean forceStop;

   public void run()
   {
     ..
     ..
     ..
     ..
   }
}

Another fact is i do not have control of logic flow implemented into method run(); which simply call some third-party program. I was trying with light inner thread class and calling method interrupt() on parent thread, but this does not work.

Please suggest any pattern….

  • 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-10T02:28:18+00:00Added an answer on June 10, 2026 at 2:28 am

    Here is final analysis…for killing thread.

    1. Using Thread to control the execution of Program B. But then stopping a process means via a thread is not allowed as the related methods are deprecated (stop/suspend etc.,)

    2. Using ThreadGroup ( to have only one thread as its member) and calling ‘destroy’ on the group. But it again falls on the same track as every thread should be stopped before the destroy operation is attempeted.

    3. Process/ProcessBuilder via Runtime seems to be the better way to obtain a process reference and call destroy(), waitFor() etc. as user @Tudor mentioned.

    Here is exact outlined code i have tried and it fails to kill, commented line while(isValid()) represents my another java program invoke.

    
    
        import java.util.ArrayList;
        import java.util.Collection;
        import java.util.Date;
        import java.util.List;
        import java.util.concurrent.Callable;
        import java.util.concurrent.CompletionService;
        import java.util.concurrent.ExecutionException;
        import java.util.concurrent.ExecutorCompletionService;
        import java.util.concurrent.Executors;
        import java.util.concurrent.Future;
        import java.util.concurrent.TimeUnit;
    
        class TaskWorker implements Callable {
            private final int number;
    
            public TaskWorker(int number) {
                this.number = number;
            }
    
            public Boolean call() throws InterruptedException {
                while (!Thread.currentThread().isInterrupted()) {
                    myBusyFunction();
                }
                return true;
            }
    
            private boolean isValid() {
                return true;
            }
    
            private void myBusyFunction() {
                //while (isValid()) {
                System.out.println(number + ">....Working.....");
                //}
            }
        }
    
        public class Test {
            public static void main(final String[] args) {
                CompletionService cs = new ExecutorCompletionService(Executors.newFixedThreadPool(1));
                Collection tasks = new ArrayList(10);
                tasks.add(new TaskWorker(1));
                List> futures = new ArrayList>(tasks.size());
                try {
                    for (Callable task : tasks) {
                        futures.add(cs.submit(task));
                    }
                    //----
                    for (int t = 0; t  result = cs.poll(10, TimeUnit.SECONDS);
                        if (result == null) {
                            System.out.println(new Date() + ":Worker Timedout:");
                            //So lets cancel the first futures we find that havent completed
                            for (Future future : futures) {
                                System.out.println("Checking future");
                                if (future.isDone()) {
                                    continue;
                                } else {
                                    future.cancel(true);
                                    System.out.println("Cancelled");
                                    break;
                                }
                            }
                            continue;
                        } else {
                            try {
                                if (result.isDone() && !result.isCancelled() && result.get()) {
                                    System.out.println(new Date() + ":Worker Completed:");
                                } else if (result.isDone() && !result.isCancelled() && !result.get()) {
                                    System.out.println(new Date() + ":Worker Failed");
                                }
                            } catch (ExecutionException ee) {
                                ee.printStackTrace(System.out);
                            }
                        }
                    }
                } catch (InterruptedException ie) {
                } finally {
                    //Cancel by interrupting any existing tasks currently running in Executor Service
                    for (Future f : futures) {
                        f.cancel(true);
                    }
                }
                System.out.println(new Date() + ":Done");
            }
        }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which runs some multi-threading process. In each thread I'm sharing
Preliminary : I have an application which maintains a thread pool of about 100
I have an application in which I am running a separate thread. Dim thread
I am building an application in swt, I also have a thread running which
I have an application where i have created a thread which must check the
We have created a multi-threaded application which process/parse big files (few hundred MB's) simultaneously.
I have an ASP.NET MVC web application which is hosted by an external provider,
Our application has a background thread which spawns a process through System.Diagnostics.Process : Process.Start(
I have an application which runs multiple threads. I use MadExcept to catch errors
I have a problem with winform application which uses threads to update the UI.

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.