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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:05:55+00:00 2026-06-05T20:05:55+00:00

This class extends Thread and once it is created, the thread is started. This

  • 0

This class extends Thread and once it is created, the thread is started. This is the code:

class Controller extends Thread implements ConfigurationObserver{

    private int refreshMS;

    //...

    @Override
    public void notifyConfiguration(ConfigurationModel config) {
        refreshMS = config.getRefreshMs();
    }

    @Override
    public void run() {
        //...
        while (true) {

            try {
                Thread.sleep(refreshMS);
            } catch (InterruptedException ex) {
                //...
            }
        }
    }
}

It follows the Observer Pattern. This class will subscribe itself to the ConfigurationController which will notify him every time there is a change in any of the configuration parameters, through notifyConfiguration(...)method.

What makes me a little insecure about this, is the attribute refresMS. Configuration is changed through the GUI (Thread #1) and affects the attribute of Controller class (Thread #2) which is read from the running thread of that class (Thread #3).

Q1: Could this become a race condition?
Q2: If so, what is it the best way to solve this problem?

  • 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-05T20:05:56+00:00Added an answer on June 5, 2026 at 8:05 pm

    The solution I did is the one pingw33n suggested. Using the keyword volatile.

    class Controller extends Thread implements ConfigurationObserver{
    
        private volatile int refreshMS;
        //...
     }
    

    Quote from Brian Goetz’s Managing Volatility

    Volatile variables share the visibility features of synchronized, but
    none of the atomicity features. This means that threads will
    automatically see the most up-to-date value for volatile variables.

    Which means that volatile could be used instead of synchronized in very few cases. But luckly this is one of them, since int is writed atomicly (no thread can read its value while other one is modifying it).

    Therefore, as Stephen C said, this does not eliminates the race condition, it only makes it really rare to happen. In my case, if the refresMS is readed with an old value by the running thread is not a big deal (if it is something that barely happens).

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

Sidebar

Related Questions

This is my simple program in Java: public class Counter extends Thread { public
I've created a class that extends Application. This class starts a service when the
Consider this class hierarchy: Book extends Goods Book implements Taxable As we know, there
In Eclipse 3.7 we could do this: public class HelloName extends AbstractHandler { @Override
I have this class: public class Source extends Node { protected DistributionSampler delay ;
I have this class for my UI public class MyFrame extends JFrame{ JTextArea textArea;
I have class A which extends the Activity class. This class is in package
Is there a way to make a class such this one: class DB extends
I have 2 files: xxxxxTest.java [ refer this ] public class xxxxxTest extends TestCase
this is my class: class toyota extends car { function drive() { } function

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.