Assignment of local temporary variables doesn't work!?

R

Rafal

Here's a code snippet:
---
public boolean isValid(Quote q)
{
String s = q.getSymbol();
double b = q.getBid();
int bs = q.getAskSize();

//...
// validation code (some if statements that's all)
//...


System.out.println("lSym=" + s + ", lBid=" + b +"lBidSize=" + bs);

System.out.println("qSym=" + q.getSymbol() + ", qBid=" +
q.getBid() +"qBidSize=" + q.getBidSize();

}
---

The problem is that sometimes the System output printouts printout
different values for member of q and local variables s, b, bs. To be
more specific, the local variables b & bs (in particular) are zero
while q.getBid() and q.getBidSize() return other values.

Has anyone come across it?
I don't want to have to use q.getXXX() for efficiency's sake. This
method is called very often in my program.
Any help would be appreciated. Thanks.

Rafal
 
A

Andrea Desole

how about the validation code? Also, is the application single threaded?
Maybe you can try to put some log messages in the getters and in the
setters of q to be sure that the values are correct and they are not changed
 
R

Rafal

Thanks for the response.
It is a multi threaded program but I tried synchronizing the whole
method, synchronizing q, and the same thing keeps happening.
unfortunately the Quote object is a part of a third party library and I
have no access to the source code.
 
T

Thomas G. Marshall

Andrea Desole coughed up:
how about the validation code? Also, is the application single
threaded? Maybe you can try to put some log messages in the getters
and in the setters of q to be sure that the values are correct and
they are not changed


Ok, a couple of things.

First, re-examine what the values are actually returning from q.mumble().

Second, reconsider the removal of the accessor methods for speed. Report
back to us the places and frequency of such calls that makes you concerned
about this. You are very likely not speeding up anything.

Most compilers are /very/ good at putting in blindingly fast code at
accessor points. YMMV, however, but remember that direct access of member
variables is /fraught/ with peril, both now, and especially down the road.

If you don't believe me, consider this:

1. what happens to the member variables is called
in an outside thread that is not completely synchronized
with other access to the variable?

2. what happens if the member variable is accessed
before it is properly assigned/computed within q?

3. what happens down the road when another person
tries to add to your code.

It is this kind of user-level over optimization that is so frowned upon in
the outside world. If you're not professional yet, you should be aware of
this.
 
T

Thomas G. Marshall

Apologies Andrea. The preceding message /should/ have been replied to the
OP, not to you.
 
T

Thomas G. Marshall

Rafal coughed up:
Thanks for the response.
It is a multi threaded program but I tried synchronizing the whole
method, synchronizing q, and the same thing keeps happening.
unfortunately the Quote object is a part of a third party library and
I have no access to the source code.


Ok, I gather what you must have meant before. You don't want to get rid of
the getters in Quote (you can't as you state in this post), but you want to
use the getter only /once/ at the beginning of the method. That was unclear
before.

There are many objects that require certain things to happen first before
their accessors return reliable results. Java's gui components for example
will not return reliable width and height until they are actually drawn by
the layout manager.

Check the documentation to see if the getters are /always/ callable or not.
 
A

Andrea Desole

Rafal said:
Thanks for the response.
It is a multi threaded program but I tried synchronizing the whole
method, synchronizing q, and the same thing keeps happening.
unfortunately the Quote object is a part of a third party library and I
have no access to the source code.
oh, that's nice
Can't you replace with some code written by you, like a stub? Maybe
something like:


class MyQuote extends Quote
{
public String getSymbol()
{
String result = realQuote.getSymbol();
System.out.println( "getSymbol called. Return is " +
result );
return result;
}

private Quote realQuote;
}

Also, you said you tried to synchronize it; how? Did you try this?

public boolean isValid(Quote q)
{
synchronized( q )
{
// do your stuff
}

return whatever;
}

Again, sorry if I repeat it, the problem might also be in the code you
omitted (if I don't see it, I don't believe it). Try to remove it (maybe
replacing it with a sleep, so that you leave the time to other threads
to change your object)
 
A

Andrea Desole

Thomas said:
Apologies Andrea. The preceding message /should/ have been replied to the
OP, not to you.
Wow, really kind of you to specify that.
Thank you, I appreciate it.
 
P

P.Hill

Thomas said:
Most compilers are /very/ good at putting in blindingly fast code at
accessor points. YMMV, however, but remember that direct access of member
variables is /fraught/ with peril, both now, and especially down the road.

FWIW, the OP did not move to accessing members directly, but used a
local to cache the result. The local seemed to contain a different
value than the getter returned at point slightly later in the code.

But, I would also caution against bothering to eliminate a get of a
simple member.

-Paul
 
T

Thomas G. Marshall

P.Hill coughed up:
FWIW, the OP did not move to accessing members directly, but used a
local to cache the result.

Which I posted three hours before this post. Then again, the "times" of
posts are hooey, so who knows when your post was really made.
 
R

Rafal

I tried everything but subclassing Quote. I'm also talking to the
library vendor and they initially informed me that there may be little
I can do. Even I do:
synchronized(q)
{
// do my work here ...
}
they said they override any lock on an object of Quote to update its
data.
Thanks for all the help anyway
 
A

Andrea Desole

Rafal said:
I tried everything but subclassing Quote. I'm also talking to the
library vendor and they initially informed me that there may be little
I can do. Even I do:
synchronized(q)
{
// do my work here ...
}
they said they override any lock on an object of Quote to update its
data.
And how can they do it? Does it mean that they keep a reference to q's
members, so that they can change them even if q is synchronized? It
looks a bit dirty here; if the vendor doesn't have a special idea (which
you are not following) about how his library should be used, maybe you
should think about writing some thread safe wrappers. With a framework
like that there is no way you can use safely more than one thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top