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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:26:57+00:00 2026-06-07T18:26:57+00:00

I am test a scenario to use volatile variable to stop one running thread

  • 0

I am test a scenario to use volatile variable to stop one running thread from another.
I wonder why its not working. Where is the problem?
My code is:

public class StoppableTask extends Thread {
    private  volatile boolean pleaseStop;

    public void run() {
       System.out.println("Running..");
       while (!pleaseStop) {
           System.out.println("Working...");
       }
    }
    public void tellMeToStop() {
        pleaseStop = true;
    }
}

public class Stopper extends Thread {
    StoppableTask t ; 

    public Stopper(StoppableTask t){
        this.t=t;
    }
    public void run(){
        System.out.println("Ok..running too..");
        try {
            System.out.println("Waiting..");
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.tellMeToStop();
        System.out.println("Done Waiting..");
    }

    public class QuickTest{

        public static void main(String[] args) {
        StoppableTask t = new StoppableTask();
        Stopper s = new Stopper(t);
        t.start();
        s.start();

    }
 }
  • 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-07T18:26:59+00:00Added an answer on June 7, 2026 at 6:26 pm

    I suspect that your program is printing so much output to the terminal that it is blocking waiting for the output to be displayed. It looks like it is not stopping but really it will. You just need to wait… for a long time…

    You should put a Thread.sleep(100); inside of of the while() spin loop in StoppableTask.run() to slow down that output. Another way to do it is to remove the System.out and just increment a counter or something.

    I just tried it and your program finishes in 5 seconds as expected:

    public void run() {
        System.out.println("Running..");
        while (!pleaseStop) {
            // System.out.println("Working...");
        }
        System.out.println("Stopped task Done");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to use AsyncProcessor in test scenario, expecting only one message to run
Following on from this question...I'm trying to unit test the following scenario: I have
I'd like to be able to use MSpec or another BDD/TDD framework to test
Problem Scripts on some pages that use jquery are not activating. Scenario Finding that
Hey, I am trying to use Mocha and Rspec to test a scenario where
Consider the following scenario: a FIFO named test is created. In one terminal window
I'm learning to use cucumber, in order to test email parsing and am running
My Cucumber scenario is used to test authentication from a third party provider. What
I need to simulate a test scenario in which I call the getBytes() method
I am new to RSpec and I have a test scenario in which I

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.