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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:49:24+00:00 2026-06-02T21:49:24+00:00

I have made a counter application where it uses threads to interrupt the count

  • 0

I have made a counter application where it uses threads to interrupt the count when the user enters “stop” into the console. I have double checked my code and I can’t see the problem. I am new to threads so can anyone please take a look at this.

import java.util.Scanner;

public class CounterInterruptApp 
{

    public static void main(String[] args) 
    {
      new CounterInterruptApp().start();
    }

    public void start()
    {
        Thread counter = new Counter(); //Instantiate the counter thread.
        counter.start(); //Start the counter thread.

        Scanner scanner = new Scanner(System.in);
        String s = "";
        while(!s.equals("stop")); //Wait for the user to enter stop.
        s=scanner.next();
        counter.interrupt(); //Interrupt the counter thread.
    }

}



public class Counter extends Thread //Extend Thread for the use of the Thread Interface.
{
    public void run()//Run method.  This is part of the Thread interface.
    {
        int count = 0;
        while(!isInterrupted())
        {
            System.out.println(this.getName() + "Count: " + count);
            count++;
            try //Try/Catch statement.
            {
              Thread.sleep(1000); //Make the Thread sleep for one second.
            } catch(InterruptedException e) {
              break;
            }
        }
        System.out.println("Counter Interrupted."); //Display the message Counter Interrupted.
    }

}
  • 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-02T21:49:29+00:00Added an answer on June 2, 2026 at 9:49 pm

    Your while loop checking for the ‘stop’ string is bad-formed.
    It should be like this:

    while(!s.equals("stop"))
    {   //Wait for the user to enter stop.
        s=scanner.nextLine();
    }
    counter.interrupt();  //Interrupt the counter thread.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made an application (for my self) for feeds reading, using SyndicationFeed ,
I have made a custom formwizard and incorporated it into my admin interface. Basically
I have made an application using Java/Hibernate/MySQL, but whenever I try running it, it
I have made Iphone applicatio. In my application whatever data i have recorded; all
I have a count register, which is made up of two 32-bit unsigned integers,
Suppose I have a variable counter, and there are several threads accessing and setting
ok well i have this error with a counter i made witch is suppost
I have made a simple realtime visitor counter. You can download it from this
I have this counter I made but I want it to run forever, it's
I try to make a diagram with RGraph(JS). I have a user counter on

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.