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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:34:10+00:00 2026-05-25T01:34:10+00:00

I have a thread: class Foo extends Thread { boolean active = true; public

  • 0

I have a thread:

class Foo extends Thread
{
    boolean active = true;

    public void run()
    {
        while(active)
        {
            //do stuff
        }
    }

    public void end()
    {
        active = false;
    }

    public void hibernate()
    {
        synchronized(this)
        {
            wait();
        }
    }
 }

If another thread calls end(), will Foo immediately see that active is now false? Specifically, because active isn’t volatile, I’m not sure that it will. I initially created end() as a clever way of avoiding volatile, but now I’m unsure that it will actually do what I intend.
Additionally, if another thread calls hibernate(), which thread will go to sleep? I’m intending Foo to sleep, so if this doesn’t do what I intend, an alternative suggestion would be very welcome.

  • 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-25T01:34:10+00:00Added an answer on May 25, 2026 at 1:34 am

    If another thread calls end(), will Foo immediately see that active is now false?

    No it won’t. Or at least, it won’t see it all of the time.

    If you want run to always see the new value immediately, there has to be a “comes after” relationship between the thread assigning to the variable and the thread reading it. This can be achieved:

    • by declaring active volatile,
    • by putting synchronized blocks around the statements that read and write the variable,
    • by making the variable an “atomic” type; e.g. AtomicBoolean, or
    • by using some other appropriate concurrency class; see the java.util.concurrent.* packages.

    … a clever way of avoiding volatile …

    Declaring the variable to be volatile is one way of ensuring proper synchronization. It is a fact that proper synchronization imposes a performance overhead. However, proper synchronization is essential for your application to work reliably, and it is NOT “clever” to avoid it.

    (Without proper synchronization, your program will probably still work most of the time, and it might even always work on some machines. However, occasionally it won’t work, and the actual behavior is likely to depend on what machine you run the program on, what the machine load is, and other things.)

    Additionally, if another thread calls hibernate(), which thread will go to sleep?

    The thread that makes the call will go to sleep. And it won’t wake up unless some other thread does a notify or notifyAll on the same Foo object.

    If you simply want the application to go to sleep and wake up a bit later, use Thread.sleep. But beware that using sleep in the wrong way can make your application slow and unresponsive.

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

Sidebar

Related Questions

Say if I have four functions as below: def foo(): subprocess.Popen('start /B someprogramA.exe', shell=True)
The Microsoft .NET Base Class Library provides several ways to create a thread and
I have two or more threads(created by subclassing threading.Thread) running in parallel and they
If I have minimum 2 classes. One class creates the Bitmap and the other
HI guys, ==>in my application i have used three different thread code for one
I have to use functionality that is in another application domain. The result should
I have written two simple Java classes (one of them containing main(), and the
general question it can be in c i guess also if i have (
I am trying to use a class based implementation of Win32 threads to create
I'm trying to make an Android game, and I am following a few code

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.