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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:46:52+00:00 2026-06-18T11:46:52+00:00

I have a background thread which will not start processing until it’s start variable

  • 0

I have a background thread which will not start processing until it’s start variable becomes true:

class MyBackgroundThread implements Runnable {
    // ...

    public void run() {
        while(true) {
            if(!start) continue;

            doSomethingWith(myValue);
        }
    }
}

The start variable is set to true from clicking a button on a JFrame which is of course running on the Event Dispatch Thread. There’s also a myValue field in the background thread class, which is set from clicking the button:

startBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        backgroundThreadInstance.setMyValue(100);
        backgroundThreadInstance.setStart(true);

        // ...
    }
});

As you can see, it assigns something to myValue before setting start to true. Does this mean that setting myValue and start as volatile is not required? Since myValue is written to first, it will be leaked to the background thread before start is, thus the background thread will never get the chance to process an uninitialised myValue?

  • 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-18T11:46:53+00:00Added an answer on June 18, 2026 at 11:46 am

    Short answer is yes. Though, in practice, eventually the change to true would likely be seen by your thread, in theory it might never happen.

    However, agree with @NamshubWriter that there are better ways to do this than an busy/idle loop. I like his proposal to set the integer and then submit it to an ExecutorService. e.g.

    public void actionPerformed(ActionEvent e) {
      BackgroundRunnableInstance runnable = new BackgroundRunnableInstance();
      runnable.setMyValue(100);  // could be in the constructor instead
      someExecutorService.submit(runnable);
    }
    

    One difference is that if they hit the button multiple times you would have several runnables started. Which may or may not be what you want.

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

Sidebar

Related Questions

I would like to have a thread running in background which will check connection
I have a thread which needs to be run in the background every 1000ms.
I have coded a background logging thread for my program, if a class needs
I have a helper class that should only ever run in a background thread.
I have a background thread loading data which I want to display in an
I have a long-running operation which I perform in a background thread. As it
Suppose I have a GUI application which has a background thread which runs for
Background : I have a few different threads which each need to write to
I have a background thread that handles communication with an external service. Each time
I have a background thread running that fires events, but how can I ensure

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.