Rachel
Rachel

Reputation: 103617

Java : Instruction Flow not clear

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();
    }
}

Upvotes: 4

Views: 304

Answers (5)

samitgaur
samitgaur

Reputation: 5661

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();
    }
}

Upvotes: 2

morisil
morisil

Reputation: 1355

It seems to be simple memoization:

http://en.wikipedia.org/wiki/Memoization

Upvotes: 3

Shawn D.
Shawn D.

Reputation: 8135

It looks like the WhatDoIDo class is like a wrapper around a class that implements interface X. The first time z is called, it sets the object it's going to return, and sets b to true, so that even if x.y() returns null, x.y() doesn't get called again.

That way, repeatedly calling z() will return the same object (or null value).

Upvotes: 0

Romain Hippeau
Romain Hippeau

Reputation: 24375

In addition to what Mike says.
The default initialization for a boolean is false (For integers, floats and doubles 0 - Objects are set to null).
What the method does is the first time it is called it will return whatever Object Y is in the X that was passed into the constructor, After that it will return the same thing since o is assigned to x.y().

Is this homework ?

Upvotes: 0

Mike
Mike

Reputation: 19737

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

This is a method with default visibility (package visible). It returns an object of type Object. It's signature contains the synchronized keyword. This instructs the compiler to disallow multiple threads to execute this method at the same time. If one thread executes the method, the method is locked, or synchronized. Other threads may not execute the method until the original thread released this implicit locking.

In short, the synchronized keyword is a semaphore mechanism built into Java. (Actually, on second thought, is this keyword actually a semaphore?)

Upvotes: 0

Related Questions