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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:22:23+00:00 2026-05-14T14:22:23+00:00

This is a problem I encounter frequently in working with more complex systems and

  • 0

This is a problem I encounter frequently in working with more complex systems and which I have never figured out a good way to solve. It usually involves variations on the theme of a shared object whose construction and initialization are necessarily two distinct steps. This is generally because of architectural requirements, similar to applets, so answers that suggest I consolidate construction and initialization are not useful. The systems have to target Java 4 at the latest, so answers that suggest support available only in later JVMs are not useful either.

By way of example, let’s say I have a class that is structured to fit into an application framework like so:

public class MyClass
{

private /*ideally-final*/ SomeObject someObject;

MyClass() {
    someObject=null;
    }

public void startup() {
    someObject=new SomeObject(...arguments from environment which are not available until startup is called...);
    }

public void shutdown() {
    someObject=null; // this is not necessary, I am just expressing the intended scope of someObject explicitly
    }
}

I can’t make someObject final since it can’t be set until startup() is invoked. But I would really like it to reflect its write-once semantics and be able to directly access it from multiple threads, preferably avoiding synchronization.

The idea being to express and enforce a degree of finalness, I conjecture that I could create a generic container, like so (UPDATE – corrected threading sematics of this class):

public class WormRef<T>
{
private volatile T                      reference;                              // wrapped reference

public WormRef() {
    reference=null;
    }

public WormRef<T> init(T val) {
    if(reference!=null) { throw new IllegalStateException("The WormRef container is already initialized"); }
    reference=val;
    return this;
    }

public T get() {
    if(reference==null) { throw new IllegalStateException("The WormRef container is not initialized"); }
    return reference;
    }

}

and then in MyClass, above, do:

private final WormRef<SomeObject> someObject;

MyClass() {
    someObject=new WormRef<SomeObject>();
    }

public void startup() {
    someObject.init(new SomeObject(...));
    }

public void sometimeLater() {
    someObject.get().doSomething();
    }

Which raises some questions for me:

  1. Is there a better way, or existing Java object (would have to be available in Java 4)?

Secondarily, in terms of thread safety:

  1. Is this thread-safe provided that no other thread accesses someObject.get() until after its set() has been called. The other threads will only invoke methods on MyClass between startup() and shutdown() – the framework guarantees this.
  2. Given the completely unsynchronized WormReference container, it is ever possible under either JMM to see a value of object which is neither null nor a reference to a SomeObject? In other words, does the JMM always guarantee that no thread can observe the memory of an object to be whatever values happened to be on the heap when the object was allocated. I believe the answer is “Yes” because allocation explicitly zeroes the allocated memory – but can CPU caching result in something else being observed at a given memory location?
  3. Is it sufficient to make WormRef.reference volatile to ensure proper multithreaded semantics?

Note the primary thrust of this question is how to express and enforce the finalness of someObject without being able to actually mark it final; secondary is what is necessary for thread-safety. That is, don’t get too hung up on the thread-safety aspect of this.

  • 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-14T14:22:24+00:00Added an answer on May 14, 2026 at 2:22 pm

    This is my final answer, Regis1 :

    /**
     * Provides a simple write-one, read-many wrapper for an object reference for those situations
     * where you have an instance variable which you would like to declare as final but can't because
     * the instance initialization extends beyond construction.
     * <p>
     * An example would be <code>java.awt.Applet</code> with its constructor, <code>init()</code> and
     * <code>start()</code> methods.
     * <p>
     * Threading Design : [ ] Single Threaded  [x] Threadsafe  [ ] Immutable  [ ] Isolated
     *
     * @since           Build 2010.0311.1923
     */
    
    public class WormRef<T>
    extends Object
    {
    
    private volatile T                      reference;                              // wrapped reference
    
    public WormRef() {
        super();
    
        reference=null;
        }
    
    public WormRef<T> init(T val) {
        // Use synchronization to prevent a race-condition whereby the following interation could happen between three threads
        //
        //  Thread 1        Thread 2        Thread 3
        //  --------------- --------------- ---------------
        //  init-read null
        //                  init-read null
        //  init-write A
        //                                  get A
        //                  init-write B
        //                                  get B
        //
        // whereby Thread 3 sees A on the first get and B on subsequent gets.
        synchronized(this) {
            if(reference!=null) { throw new IllegalStateException("The WormRef container is already initialized"); }
            reference=val;
            }
        return this;
        }
    
    public T get() {
        if(reference==null) { throw new IllegalStateException("The WormRef container is not initialized"); }
        return reference;
        }
    
    } // END PUBLIC CLASS
    

    (1) Confer the game show “So you want to be a millionaire”, hosted by Regis Philburn.

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

Sidebar

Related Questions

i've actually never encounter this problem before. I usually start designing my layout in
During coding I frequently encounter this situation: I have several objects ( ConcreteType1 ,
I encounter this problem : I have a django (python) server that serve XML
I have encountered this problem today and I don't have an explanation for it.
While working on my final project for my AS/400 Course, I encountered this problem
This is not a question, more like sharing with others a problem I encountered
Its my first time to encounter this problem so please anyone help me. Here's
I frequently encounter this situation in my VB6 applications Private Sub DoSomething On Error
I encounter this problem Fatal error: Uncaught exception 'PDFlibException' with message 'Couldn't find encoding
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to

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.