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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:56:09+00:00 2026-06-12T11:56:09+00:00

There are many threads on SO about interrupting reading the system.in but what I

  • 0

There are many threads on SO about interrupting reading the system.in but what I am looking for here is some kind of advice as to how to best code what I am trying to achieve.

I have a getlogin() method that needs to do the following: ask a user to input the desired login environnement details, if after 6 seconds user have not input a valid value (“live” or “test”) then set userlogin variable to “test” and return it to the caller.

I have taken the following approach for the getlogin() implementation:

  1. launch two threads which do the following:

    • thread1 creates a scanner object then calls scanner.nextline() and depending on user input set a variable userlogin. Interrupts thread2 before exiting thread1.
    • thread2 waits 6 seconds and if after that userlogin is still not set, then set a default value for userlogin. Interrupts thread1 before exiting thread2.
  2. join thread2 to stop main Thread from returning userlogin as null

  3. return userlogin

The problem I have with the approach is that scanner.nextline() does not interrupt when thread2 calls thread1.interrupt, which is why I do not join thread1 in step 2 as the main Thread would hang.
Is there a way to get thread1 to complete after thread2 interrupts it? Or else is this approach completely overkill and there is a much simpler way to achieve the contract?

  • 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-12T11:56:10+00:00Added an answer on June 12, 2026 at 11:56 am

    The simplest solution is to expose the underlying stream in the “reading” thread and close that stream from the timeout thread. This should interrupt the reading and raise an exception. Handle this exception and you should be able to proceed with your logic. The only gotcha is that you won’t be able to re-use the same stream again. Unfortunately there is no easy way to deal with interruption of blocking system calls.

    EDIT:

    Following a completely different line of reasoning; given that we can’t close the input stream just to interrupt it, the only way I can think of is to use the “programmatic user input” facilities offered by the Robot class. Here is an example which works out for me:

    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import java.util.Scanner;
    import java.util.concurrent.TimeUnit;
    
    public class ConsoleTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            new TimeoutThread().start();
            new ReaderThread().start();
        }
    
    }
    
    class ReaderThread extends Thread {
    
        @Override
        public void run() {
            System.out.print("Please enter your name: ");
            try(Scanner in = new Scanner(System.in)) {
                String name = in.nextLine();
                if(name.trim().isEmpty()) {
                    name = "TEST"; // default user name
                }
                System.out.println("Name entered = " + name);
            }
        }
    
    }
    
    class TimeoutThread extends Thread {
    
        @Override
        public void run() {
            try {
                Thread.sleep(TimeUnit.SECONDS.toMillis(5));
                Robot robot = new Robot();
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyRelease(KeyEvent.VK_ENTER);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    

    The above code uses the logic that once that timeout has expired, we simulate a newline which will cause the “name” variable to be blank. Then we have a check which performs the necessary logic and sets the appropriate user name.

    The gotcha about the above approach is that it:

    • Uses Robot class of AWT so might not play well with headless terminals (?)
    • Assumes that the focus window is the console window. If the focus is somewhere else, the ENTER key-press will be registered for that window as opposed to your application window.

    Hope this helps you out. I’m really out of ideas now. 🙂

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

Sidebar

Related Questions

There are many threads about going the opposite way, but I am interested in
I know there are many threads that talk about this problem but I don't
There are many threads and samples out there about typeahead ( https://gist.github.com/1866739 ) and
I have a black box of code that contains many threads. There is no
There are many errors here in SO, but this scenario I think its different.
I know there are many threads about calculating from lat/long to metric systems. This
Here is what I want to do: There are many threads start at any
I've read many docs about thread states, some of them tells that there is
There are many threads in the forum about the Friends and Templates. I specially
Is there any way to check how many threads are waiting for a synchronized

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.