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 9235961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:10:34+00:00 2026-06-18T07:10:34+00:00

I have a task x that is executed continuously in a thread which will

  • 0

I have a task x that is executed continuously in a thread which will only stop when the boolean changes it’s state to true. I have done some reading and there are 3 ways that I approach when killing threads that are in the code below. Which of the 3 methods is effective ? And if none of them aren’t effective or correct kindly suggest a proper approach with some code for reference.

Below is the code :

public class MyTest {

private static class transaction {

     private String param1,param2,param3, param4, param5;

     public transaction (String param1,String param2,String param3,String param4,String param5){
         this.param1=param1;
         this.param2=param2;
         this.param3=param3;
         this.param4=param4;
         this.param5=param5;
     }

     public String getParam1(){
         return this.param1;
     }

     public String getParam2(){
         return this.param2;
     }

     public String getParam3(){
         return this.param3;
     }

     public String getParam4(){
         return this.param4;
     }

     public String getParam5(){
         return this.param5;
     }
}

public static void processBatch(String workerName){

    try{

        java.util.List <transaction> transactions= new java.util.LinkedList<transaction>();
        java.sql.ResultSet dbtrx=Database.db.execQuery((Object)"dbname.procname");

        while(dbtrx.next()){// Takes a snapshot of the pending payments in the table and stores it into the list.
            Object obj=new transaction (dbtrx.getString("col1"), dbtrx.getString("col2"), dbtrx.getString("col3"), dbtrx.getString("col4"), dbtrx.getString("col5"));
            transactions.add((transaction)obj);
            obj=null;
        }

        java.util.Iterator<transaction> iterate= transactions.iterator();

        /* Processes the pending batch payments*/
        while(iterate.hasNext()){
            transaction trx=iterate.next();
            /*Calls posting function here*/
            System.out.println(workerName+":- Param1 : "+trx.getParam1()+" - Param2 : " +trx.getParam2()+
                    " - Param3 : "+ trx.getParam3()+" - Param4 : "+ trx.getParam4()+" - Param5 : "+ trx.getParam5());
            iterate.remove();
        }

        /*cleaning object references*/
        dbtrx=null;
        transactions=null;
        iterate=null;

     }catch(Exception e){
        e.printStackTrace();
     }
}

public static void main(String [] args) throws InterruptedException{
    volatile boolean stop=false;
    Object hold= new Object();
    new Thread("Batch Worker A"){
        @Override 
        public void run(){
            while(true){
                if(stop!=true){
                    processBatch(Thread.currentThread().getName());
                }else{
                    try{
                    Thread.sleep(0);
                    Thread.currentThread().interrupt();
                    }catch(java.lang.InterruptedException e){
                        Thread.currentThread().interrupt(); 
                        break;
                    }
                }
            }
        }}.start();

    new Thread("Batch Worker B"){
        @Override 
        public void run(){
            try{
                while(stop!=true){
                    processBatch(Thread.currentThread().getName());
                }
                Thread.sleep(0);
                Thread.currentThread().interrupt();
            }catch(java.lang.InterruptedException e){
                Thread.currentThread().interrupt(); 
            }
        }}.start();

    new Thread("Batch Worker C"){
        @Override 
        public void run(){
            while(!Thread.currentThread().isInterrupted()){
                if(stop!=true){
                    processBatch(Thread.currentThread().getName());
                }else{
                    Thread.currentThread().interrupt();
                }
            }
        }}.start();
    }
}

}

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

    In all of your examples, you aren’t really killing the thread, you are stopping the batch from processing more items.
    To understand the difference, note that none of your methods would actually stop the thread while the thread is within the processBatch function.

    There are some things to take note of:

    • There is no point in calling Interrupt() on your current thread. The idea behind Interrupt is for external threads to call it. In your case, you can just as well throw an exception, or return from the run() function (which would shut down the thread automatically).
    • Even interrupt() can’t in many situations stop a thread if that thread is locked outside java ,such as thread waiting for IO (if not using NIO), including a socket, which is what the database connection is, you’ll need to design a different way to stop a thread inside IO (usually by doing a timeout, but there are other ways).

    if you goal is simply to stop the next batch from happing use the code from Joonas :

    new Thread("Batch Worker A"){
    @Override 
    public void run() {
        while(!stop){
                processBatch(Thread.currentThread().getName());
            }
        }
    }}.start();
    

    if your goal is to interrupt the process while running the batch, you can just as well do:

    public static void main(String[] args) {
       var t =new Thread("Batch Worker A"){
          @Override 
          public void run() {
             processBatch(Thread.currentThread().getName());
           }
       }.start(); 
       t.interrupt();
    }
    

    in general interrupt is the preferred method, and using a local scoped variable and anonymous classes is a really bad idea (use a static variable, or better an injected interface with a function to check if the thread should continue).

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

Sidebar

Related Questions

I have a task that needs to be executed on a schedule. (It basically
I have a regular task that has to be executed once per day (but
I have the problem that an specific step in Ant can only be executed
I have a task that creates new activerecord records which I have recently moved
I have an MSBuild task that executes (among other things) a call to xcopy.
I have a script task that executes a dynamically created SQL restore statement. This
I have a periodic task that needs to execute once a minute (using delayed_job).
My goal is to have an AsyncTask that can execute multiple times (one task
I have a task that runs periodically 10 second. I do some picturebox refreshing
I have a task that takes a rather long time and should run in

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.