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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:00:39+00:00 2026-05-14T23:00:39+00:00

I have a Main Program which is running a script on the target device(smart

  • 0

I have a Main Program which is running a script on the target device(smart phone) and in a while loop waiting for stdout messages. However in this particular case, some of the heartbeat messages on the stdout could be spaced almost 45secs to a 1minute apart.

something like:

stream = device.runProgram(RESTORE_LOGS, new String[] {});
stream.flush();
String line = stream.readLine();
while (line.compareTo("") != 0) {
    reporter.commentOnJob(jobId, line);
    line = stream.readLine();
}    

So, I want to be a able to start a new interruptible thread after reading line from stdout with a required a sleep window. Upon being able to read a new line, I want to be able to interrupt/stop(having trouble killing the process), handle the newline of stdout text and restart a process.

And it the event I am not able to read a line within the timer window(say 45secs) I want to a way to get out of my while loop either.

I already tried the thread.run, thread.interrupt approach. But having trouble killing and starting a new thread.

Is this the best way out or am I missing something obvious?

  • 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-05-14T23:00:39+00:00Added an answer on May 14, 2026 at 11:00 pm

    It looks like the implementation of System.in varies considerably across platforms and, in particular, doesn’t always offer interruptibility or asynchronous closure.

    Here is a workaround that doesn’t rely on those features, but at the cost of failing to clean up properly; if input isn’t received before the timeout expires, the Consumer thread is left in a blocking read().

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.concurrent.SynchronousQueue;
    import java.util.concurrent.TimeUnit;
    
    class InterruptInput
    {
    
      private static final String EOF = new String();
    
      private final SynchronousQueue<String> pipe = new SynchronousQueue<String>();
    
      private final BufferedReader input;
    
      private final long timeout;
    
      InterruptInput(BufferedReader input, long timeout)
      {
        this.input = input;
        this.timeout = timeout;
      }
    
      public static void main(String... argv)
        throws Exception
      {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        InterruptInput input = 
          new InterruptInput(in, 5000);
        input.read();
      }
    
      void read()
        throws InterruptedException
      {
        System.out.println("Enter lines of input (or empty line to terminate):");
        Thread t = new Consumer();
        t.start();
        while (true) {
          String line = pipe.poll(timeout, TimeUnit.MILLISECONDS);
          if (line == EOF)
            break;
          if (line == null) {
            System.out.println("Input timed-out.");
            t.interrupt();
            break;
          }
          System.out.println("[input]: " + line);
        }
      }
    
      private class Consumer
        extends Thread
      {
    
        Consumer()
        {
          setDaemon(true);
        }
    
        @Override
        public void run()
        {
          while (!Thread.interrupted()) {
            String line;
            try {
              line = input.readLine();
            }
            catch (IOException ex) {
              throw new RuntimeException(ex);
            }
            try {
              if ((line == null) || (line.length() == 0)) {
                pipe.put(EOF);
                break;
              }
              else {
                pipe.put(line);
              }
            }
            catch (InterruptedException ex) {
              break;
            }
          }
        }
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you let jQuery manange your handlers, you could do… May 16, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer Add the following line to your routing table: routes.MapRoute("RouteName", "Help/{Thing}/{OtherThing}",… May 16, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer No, you're not using them correctly. Take a look at… May 16, 2026 at 12:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

We have a java application which run as server running on a remote windows
I'm embedding Python interpreter to a C program. However, it might happen that while
I had a Java program that I run thousand times based on a loop
I have a Java program with code: public class Test1 { public static void
I am trying to implement a program which calculates the mean and moment by
Assume that i have written a program in C++ without using RTTI and run-time
I am writing a GTKmm window program; the main window creates two buttons, one
I have a program that reads a raw list of in-game entities, and I
I have a generic abstract base class from which I would like to derive
Ok I have been trying every which way to figure this out. I need

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.