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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:15:59+00:00 2026-05-16T03:15:59+00:00

I was going through some random java code and came across this peice of

  • 0

I was going through some random java code and came across this peice of code, I am trying to understand the flow and having hard time understanding how the actual implementation of the class, actual operation done by the class so my basic questions is WhatDoIDo class actually do ? Any guidance would be apprecaited.

Q:
What would be the unit test case which explains the improved performance because of implementing in the concurrent environment.

Code

public class WhatDoIDo{
    private X x;
    private boolean b;
    private Object o;

    public WhatDoIDo(X x) {
        this.x = x;
    }

    synchronized Object z() {
        if (!b) {
            o = x.y();
            b = true;
        }
        return o;
    }

    public interface X {
        Object y();
    }
}
  • 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-16T03:16:00+00:00Added an answer on May 16, 2026 at 3:16 am

    WhatDoIDo is a wrapper class that wraps an object o.

    It defines an inner interface X which has a method y() to create an Object. This interface can be thought of as a strategy for creating the object. When an object of WhatDoIDo is created using new, it’s constructor is supplied with an object of X that will be used to create the object.

    It creates the wrapped object and makes it available to client code through z() method. It creates the object lazily. It uses a boolean flag b to keep track of whether the object has been created or not. When z() is called by client to get hold of the wrapped object, if the flag is set, the object o is returned. If flag is not set, an object is created using the strategy X supplied when creating this WhatDoIDo object. A reference to the created object is stored and returned to the client. Also, z() is synchronized since it creates the object if it has not been created already. If it was not synchronized, two threads could end up creating an object each and only one of them will get stored.

    public class ObjectWrapper {
        private CreationStrategy strategy;
        private boolean objectCreated;
        private Object wrappedObject;
    
        public ObjectWrapper(CreationStrategy strategy) {
            this.strategy = strategy;
        }
    
        synchronized Object getWrappedObject() {
            if (!objectCreated) {
                wrappedObject = strategy.createObject();
                objectCreated = true;
            }
            return wrappedObject;
        }
    
        public interface CreationStrategy {
            Object createObject();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Going through some documentation on modifying CGImageRef data, I came across a strange example
I was going through some practice coding problems , and i came across one
I'm going through some code that uses the Oracle OCI and I've come across
I was going through some code at work and I found this inline FLAGS
So I've been going through some code, and there's some things I can't understand.
Going through some tutorials about sockets, I keep coming across code like that: while(true){
I've been going through some iOS examples online I came across an app where
I am going through some code uses the name method of a Java Enum.
I am going through some Java code and I see lots of abstract classes
While going through some tutorials, I have encountered lines such as this: ((IDisposable)foo).Dispose(); Ignore

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.