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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:39:29+00:00 2026-05-28T19:39:29+00:00

public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker

  • 0
    public class InterruptedInput {

    public static void main(String[] args) {
        InputThread th=new InputThread();  //worker thread instantiated.
        th.start();  //worker thread start.
        try{
            Thread.sleep(5000);  //main thread sleep for 5 sec.
        }catch(InterruptedException ex){}
        th.interrupt();  //worker thread interrupted.
    }
}

class InputThread extends Thread{
    int sum;
    BufferedReader br;
    public void run(){
        br=new BufferedReader(new InputStreamReader(System.in));
        try{
            sum();
            sleep(10000);  //worker thread sleep for 10 sec.
        }catch(Exception ex){
            System.out.println("sum="+sum);
        }
    }

    public void sum(){
        try{
            while(!isInterrupted()){
                sum+=Integer.parseInt(br.readLine());
            }
        }catch(Exception ex){}
    }
}

In this program, the worker thread waiting for the input by the user (which will be an integer value) and sum up until the main thread interrut() it.
But when the main thread interrupt() the worker thread its on I/O for user input and not interrupting.

The output I want to come up with this program is:
when the program executed, user have to input the integer values and after five sec. the sum of the input values is printed either the user still inputing the values or waiting for 5sec gone off. The result will be printed right after the 5th sec.

  • 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-28T19:39:30+00:00Added an answer on May 28, 2026 at 7:39 pm

    OK so the problem with this code is the fact that the statement br.readLine() must be executed before the loop conditional is checked. So to avoid this I would have 3 threads potentially.

    Thread 1:

    This would be the main or parent thread. In this thread you will merely count to 5 and then interrupt thread 2.

    Thread 2:

    In this thread you will monitor the static variable: count,then print it and interrupt thread 3 when interrupted.

    Thread 3:

    Here is were you would get input and and pass the input into a function which would add it to count. When interrupted do nothing.

    If you have any questions let me know.

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

Sidebar

Related Questions

public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE;
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public class Test { public static void main(String[] args) throws Exception { String s1
public class Main { public static void main (String args[]) { int nums[]= {2,
public class Main { public static void main(String[] args) throws InterruptedException { ClassA a
public class Guess { public static void main(String[] args){ <sometype> x = <somevalue>; System.out.println(x
public class Anagram { public static void main(String[] args) { String a = Despera

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.