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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:17:57+00:00 2026-06-03T05:17:57+00:00

I am new to Java and using a code given by someone. There, at

  • 0

I am new to Java and using a code given by someone. There, at the end of the code, they interrupt a thread if it has not finished. I am measuring the timing of the code.

The problem is that the Java code first issues all the threads, and then at the end it interrupts. Is interrupting necessary? Can’t we wait till all threads actually finish? Or may be just skip interrupting (these threads are running processes using process exec command and they will finish anyway).
Here is the relevant code. First the code for individual thread:

  String commandString = "./script.scr ";
  process = Runtime.getRuntime().exec(commandString);
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  while ((lsString = bufferedReader.readLine()) != null)
        {
            System.out.println(lsString);
        }       
        try
        {
            process.waitFor();
        }

Now the code of the part which dispatches these threads:

public void stopWhenAllTaskFinished()
{
    while(notFinished) {sleep(50);} //notFinished is class variable and somewhere else it will set to false. 
    //now finished.
  //Interrupt all the threads
    for (int i=0; i<nThreads; i++) {
        threads[i].interrupt();
    }
}

This function is called from main class like:

 obj.stopWhenAllTaskFinished()

I greatly appreciate any insight or answer.

  • 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-03T05:17:58+00:00Added an answer on June 3, 2026 at 5:17 am

    It is not clear whether the interrupts are needed from the code that you have posted.

    • If notFinished is set to false when all threads have finished, then the interrupts are unnecessary.

    • If notFinished is set to false when there is a possibility that some threads haven’t finished, then it could be necessary. Whether it is actually necessary depends on other things:

      • Are you prepared for the application to wait for the threads to finish naturally?
      • Can you be sure that the threads will finish? (Could they hang / block forever?)
      • Do you need the results of their activity?
      • Will they respond to an interrupt? (In this case, I think that the answer is “yes”.)
      • Is there any harm in leaving the threads there? (For instance, does it stop the application shutting down?)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im developing a simple console Application using java. The code is given below `
I'm new to using logcat and debugging in Android. I've analyzing someone else's code
I'm new to using Java Enums and I've read that replace IF logic that
Executive summary: how do I install a new root certificate into Java using Java
Good day! I'm new in using Java DB (Derby). I want to embed it
I'm new to website development using Java but I've got started with Wicket and
I am completely new to java and I am using Solr search server. It
I am new to Java world but I am pretty good at using Flex,
we are currently working on a new web application using Java and MySql. We
I am new to Java web-services and am working on one currently, using Apache

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.