Skip to main content

Performance Question

22 replies [Last post]
devuser007
Offline
Joined: 2009-01-17

Hi Friends,

I came across this question and was unable to answer this. Can anyone help me regarding this question?

This is the given Class

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

Here were the questions asked.

1. What does this class do?
2. Rename the class and its members in such a way that it is more clear what this class does.
3. Generify the class.
4. Improve performance of the class in concurrent environment using the Java Concurrency package.
Explain why the performance is better.
5. Write a test which demonstrates performance improvement in a concurrent environment.

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
peter__lawrey
Offline
Joined: 2005-11-01

> What does Generify the class mean here?
Make use of Java Generics.

devuser007
Offline
Joined: 2009-01-17

So you mean to say that i just need to answer question 1 as the class lazily evaluates x.y().
Thats it.?

peter__lawrey
Offline
Joined: 2005-11-01

> So you mean to say that i just need to answer
> question 1 as the class lazily evaluates x.y().
> Thats it.?
That's all it does. I suggest you read up on what lazy evaluation as you will be able to find other implementations which will help you answer the other questions.

devuser007
Offline
Joined: 2009-01-17

So is it good or bad to lazily evaluate?

peter__lawrey
Offline
Joined: 2005-11-01

> So is it good or bad to lazily evaluate?
Its usually a needless complication IMHO. It makes more sense in functional languages where it is directly supported and so can actually make things simpler. However in Java it adds complexity which often be solved another way.
You should be able to find plenty of discussion on the pluses and minuses of lazy evaluation.

devuser007
Offline
Joined: 2009-01-17

Um I c..! thats a good info.! So how do you go about question 2? i mean how come renaming the members will make clear what the class actually does?

peter__lawrey
Offline
Joined: 2005-11-01

You need to edit the code to replace the classes and methods with names of what they do.
I suggest you start by suggesting better names and change the code later.

devuser007
Offline
Joined: 2009-01-17

How bout this renaming?

public class LazyInvoker {
private X x;
private boolean flag;
private Object o;

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

synchronized Object method() {
if (!flag) {
o = x.function();
flag = true;
}
return o;
}

public interface X {
Object function();
}

}

devuser007
Offline
Joined: 2009-01-17

How bout this generics?

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

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

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

public interface T {
Object y();
}
}

Is the interface part right?

Thanks

peter__lawrey
Offline
Joined: 2005-11-01

Try this,

public interface X {
T y();
}

devuser007
Offline
Joined: 2009-01-17

Here is what i Did

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

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

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

public interface X {
T y();
}

}
I get a compile time error at o = x.y();
cause The method y() is undefined for the type T.

:( Between you dint give me feedback for renaming stratergy?
Do you have any messenger where i can chat with you and get this done?

peter__lawrey
Offline
Joined: 2005-11-01

> I get a compile time error at o = x.y();
> cause The method y() is undefined for the type T.
I believe you changed it to T, I suggest you change it back to X as it was or even X

You still need to figure this out for yourself.

darkdog74
Offline
Joined: 2009-04-17

I red all thread and have a question...
In this code example, you peter, said it could be used Lock...
I'm new in java.util.concurrent package and if you could post the code corrected using Lock i really appreciate it so i understand better how use it...
I red also something about java.util.concurrent.atomic and i think it could be used also that in this example, isn't it? If yes, how?
Thank you very much for your time, have a nice day and sorry for my bad english:)

wooler
Offline
Joined: 2009-04-21

Hi,
Well all question whom we have no any answer that we must contact related person who know better about these questions.

peter__lawrey
Offline
Joined: 2005-11-01

> 1. What does this class do?
Lazy evaluation of x.y()
> 2. Rename the class and its members in such a way that it is more clear what this class does.
I will leave that to you.
> 3. Generify the class.
Again, I suggest you try that.
> 4. Improve performance of the class in concurrent environment using the Java Concurrency package.
You could use a concurrency Lock, but it doesn't guarantee improved performance.
> Explain why the performance is better.
I bit hard given it might not.
> 5. Write a test which demonstrates performance improvement in a concurrent environment.
That's a good exercise for you as well.

devuser007
Offline
Joined: 2009-01-17

Thanks a ton for your reply.! I would appreciate if you could answer the rest as I am not confident on the synchronize thingy.

peter__lawrey
Offline
Joined: 2005-11-01

> Thanks a ton for your reply.! I would appreciate if
> you could answer the rest as I am not confident on
> the synchronize thingy.
You won't be until you can answer those questions for yourself.
I suggest you have a try at it and then we can make suggestions or give hints.

devuser007
Offline
Joined: 2009-01-17

I have to submit this asap else i am in deep trouble so i posted this here.
I do understand you are right. The reason i posted this over here is as I am running short of time. Do you have any messenger? prolly i would like to discuss it with you.

Thanks

peter__lawrey
Offline
Joined: 2005-11-01

I suggest you try to do as much as you can with the time you have left.
At least attempt number 2.
Who ever is marking this won't expect a perfect answer if you haven't been taught this before.

devuser007
Offline
Joined: 2009-01-17

What does Generify the class mean here?

devuser007
Offline
Joined: 2009-01-17

Here is my answer for question 1. Please correct me if I am wrong.

Synchronized Object z() obtains coarse grained lock which affects performance adversely.
This class is using a lock on a method level which is too coarse. There is no need to lock up a piece of code that does not access any shared resources by locking up an entire method.

peter__lawrey
Offline
Joined: 2005-11-01

> Here is my answer for question 1.
>> 1. What does this class do?
> Synchronized Object z() obtains coarse grained lock
> which affects performance adversely.
> This class is using a lock on a method level which is
> too coarse. There is no need to lock up a piece of
> code that does not access any shared resources by
> locking up an entire method.
I don't think your answer relates to the question. I have already told you what the class does. You just have to put it in your own words.