Java: fuzzy command flow

I went through some random Java code and came across this piece of code, I try to understand the flow and not understanding how the real implementation of the class is, the actual operation performed by the class, so WhatDoIDo my main questions WhatDoIDo class actually do? Any guidance would be appreciated.

Q: What will be the case of unit test, which explains the improved performance due to implementation in a parallel environment.

the 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 = xy(); b = true; } return o; } public interface X { Object y(); } } 
+4
source share
5 answers

WhatDoIDo is a wrapper class that wraps an o object.

It defines the internal interface X , which has a y() method for creating an Object . This interface can be considered as a strategy for creating an object. When a WhatDoIDo object is created using new , the constructor is provided with an X object that will be used to create the object.

Creates a wrapped object and makes it available to client code using the z() method. This creates an object lazily. It uses the boolean b flag to track whether an object was created or not. When z() is called by the client to capture the wrapped object, if the flag is set, the object o returned. If the flag is not set, the object is created using the X strategy, which is provided when this WhatDoIDo object is WhatDoIDo . The link to the created object is saved and returned to the client. In addition, z() is synchronized since it creates an object if it has not already been created. If it has not been synchronized, two threads can eventually create an object each, and only one of them will be saved.

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

This seems to be a simple memoization:

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

+3
source
 synchronized Object z() { if (!b) { o = xy(); b = true; } return o; } 

This is the default visibility method (visible package). It returns an object of type Object. This signature contains a synchronized keyword. This instructs the compiler to prohibit multiple threads from executing this method simultaneously. If one thread executes this method, the method is blocked or synchronized. Other threads may not execute this method until the original thread releases this implicit lock.

In short, the synchronized keyword is a semaphore engine built into Java. (Actually, in a different way, is this keyword actually a semaphore?)

0
source

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 this method does is the first time it is called, it will return any object Y to X that has been passed to the constructor. After that, it will return the same, since o is assigned xy ().

Is this homework?

0
source

It looks like the WhatDoIDo class looks like a wrapper around a class that implements the X interface. The first time z is called, it sets the object it returns and sets b to true, so even if xy () returns null, xy () is no longer called .

Thus, calling z () repeatedly will return the same object (or a null value).

0
source

All Articles