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 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

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Article : Grid View with fixed header For the gridview… May 16, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer I found this website with a nice template supporting factory… May 16, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer You should consider using arrays instead, as those dynamic variables… May 16, 2026 at 6:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I was going through someone's code where I came across a thread: while(TRUE) {
I was going through some code that I downloaded off the internet ( Got
I recently was given some code in c# ASP.NET and I was going through
I am just trying to learn some Lisp, so I am going through project
So this morning I posted a confused question about assembly and I received some
I'm currently going through a few tutorials to get myself up and running on
Recently, I have been going through search trees and I encountered red-black trees, the
I'm going through the full tutorial at cplusplus.com, coding and compiling each example manually.
I've been going through FB docs but I think I am totally lost now.
So I am going through the Android development tutorial and I have run into

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.