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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:41:14+00:00 2026-06-10T13:41:14+00:00

this class is response to execute the command ,the print the result public class

  • 0

this class is response to execute the command ,the print the result

    public class ExecutorTask implements Runnable{

    @Override
    public void run() {

        Process process = null;
        try {
            process = Runtime.getRuntime().exec("cmd /c dir");
             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
             String line="";
             while ((line = reader.readLine()) != null) {
                 System.out.println(line);
             }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            return;
        }
    }
}

the second class is a executor to run the shell use a thread

public final class ShellCommandExecutor{

    public void execute(String command){

        ExecutorTask task = new ExecutorTask();
        Thread executorThread = new Thread(task);
        executorThread.start();

        /*try {
            Thread.sleep(1000);
            executorThread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }

}

the problem is why i must in the class ShellCommandExecutor add code snippet:

try {
        Thread.sleep(1000);
        executorThread.interrupt();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

then can i see the print result:

2012-08-21  00:32    <DIR>          .
2012-08-21  00:32    <DIR>          ..
2012-08-21  00:32             1,576 .classpath
2012-08-21  00:26             1,224 .project
2012-08-07  10:58    <DIR>          .settings
2012-08-24  15:19            10,965 pom.xml
2012-08-07  10:57    <DIR>          src
2012-08-21  00:32    <DIR>          target
2012-08-24  10:22                 0 velocity.log

why?

  • 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-10T13:41:15+00:00Added an answer on June 10, 2026 at 1:41 pm

    You started a thread with

     executorThread.start();
    

    if you do nothing else the thread that started it (your main thread) will not wait for your executorThread to finish before returning, so your application will exit before this thread has executed its task.

    To wait for your executorThread to finish you should call:

    executorThread.join();
    

    later in the code. At this point you will be ensured that it has finished its task.

    Currently it works because you wait for 1 second in your main thread, during this second your other thread performs its action. But if your executorThread needed more than one second to perform it it will not work, so you should not sleep() in this case.

    See Thread.join javadoc.

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

Sidebar

Related Questions

I want to select an anchor inside a div like this <div class=response content>
Consider this class hierarchy: Book extends Goods Book implements Taxable As we know, there
Given this class public partial class Default : Page { private IRepository repo; ...
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Take this class as example: public class Category : PersistentObject<int> { public virtual string
So I have this loader class which extends AsyncTask. Then I do new loader().execute();
I have found this code sample import org.apache.http.params.CoreProtocolPNames; import org.apache.http.util.EntityUtils; public class PostFile {
I have this code so far: private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
This class hangs at Future.get() method after executing ExecutorService.shutdownNow() called. I don't know what
This class extends Thread and once it is created, the thread is started. This

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.