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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:53:56+00:00 2026-05-25T13:53:56+00:00

I have the following code that gets initialized as a static variable in a

  • 0

I have the following code that gets initialized as a static variable in a class:

public class MyXlet extends Xlet {
   boolean connected = false;
   ...

   void connect() {
      // some code goes here, starts a new thread
      MyXlet.connected = true;
   }

   void disconnect() {
      // some code goes here, the new thread is designed to terminate once connected is false;
      MyXlet.connected = false;
   }
}

Let’s say I have already run the connect method, which spawns a new thread. The disconnect() method sets “connected” to “false”. Is it guaranteed that the thread that was spawned from the connect() method will see that “connected” is no longer equal to “true”? Or will I have to use the volatile keyword on “connected” for this? It is worthy to note that I am using Java 1.4.2.

  • 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-25T13:53:57+00:00Added an answer on May 25, 2026 at 1:53 pm

    Is it guaranteed that the thread that was spawned from the connect()
    method will see that “connected” is no longer equal to “true”?

    Only if the thread that was spawned from the connect() method is the one that sets connected to false!

    The spawned thread will be able to see that connected is true after it is started, because starting a thread is a happens-before action, and source code also establishes a happens-before ordering within a thread.

    But, if the parent thread clears the connected flag after calling start() on the spawned thread, the spawned thread is not guaranteed to see the change unless you declare the flag as volatile.

    The main difference between 1.4 and 1.5 behavior is that writing to a volatile variable will also flush writes to non-volatile variables from Java 5 onward. (Reading a volatile variable also clears any cached non-volatile variable values.) Since it appears that you only have one variable involved, this change shouldn’t affect you.

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

Sidebar

Related Questions

In PHP I have the following code that gets a file submitted through CGI:
I have the following code in a Runnable that gets passed to a thread
I have a line of code that gets the following error when run through
I have following code snippet that i use to compile class at the run
I have the following code that gets called after the view loads. It works
I have the following lines of code in C# that gets data using DataTables
I have the following code that gets the value of a drop down list
I have the following question: I got some code that gets called when an
I have the following code that gets the first 'name' in an array called
I have following code that does not work due to a being a value

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.