Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
"I'm not back." - Bill Harding, Twister
Famous last words.Originally posted by Madhav Lakkapragada:
Vector is already a syncronized class.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
That's not enough. For example, the call to contents.size() in getList() and the return statement in getList(int) need to be atomic - if the contents Vector is changed between the two, you may end up with incorrect results. Unless you take a copy of contents first so you know that it cannot be changed.Originally posted by Jim Yingst:
I'd synchronize all access to the underlying contents vector - the easiest way is to synchronize both getList(int) and refresh().
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
No. Only if there's a corresponding wait().Originally posted by Mike Curwen:
Is it *always* necessary to call notify() or notifyAll() at the end of a synchronized method?
What the author probably means to say is that you, as a programmer, may face the situation that you are in a synchronized block but a necessary condition has not been met. You can then code a wait() to release the monitor lock and wait for the condition to be satisfied. For example, in a classic producer/consumer problem the consumer will generally need to wait() for the producer to come up with something.What was confusing was this: "A thread executing in a synchronized method may determine that it cannot continue, so the thread voluntarily calls wait()".
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
They can't. Apologies if I wasn't clear enough - I gave two alternative approaches, one which doesn't scale well under very high loads, another which does.Originally posted by Mike Curwen:
I'm not sure how multiple refreshes can be occuring if I synchronize all of the getList() methods, as these are the only methods to call refresh().
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Whoa. What I meant is taking a copy of the reference. I.e. a simple assignmentAnd from that moment on, the call will just operate on "mycontents" and ignore what happens to "contents". That way, you won't have to worry about other threads refreshing "contents" anymore.Originally posted by Mike Curwen:
Ok, my head is starting to hurt...
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Don't remind me please I hate it when a discussion turns personal.Originally posted by Mike Curwen:
That was what I thought you meant at first, until I remembered the whole thing about pass by reference/value.
Yep, you're using the same Vector - but, at least in the code you gave, that Vector is never modified. There are just two things that happen to the contents Vector: (1) a sublist is extracted and a copy is returned, so clients cannot touch the original whatever they do; (2) from time to time a new Vector is constructed and assigned to "contents". This, too, leaves the original Vector object unchanged.If I do that simple assignment, am i not still using the Vector on the outside of the method call?
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Since you decided to go for the first, simple, low-to-moderate load option, you can safely forget all about mustRefresh(). But, for academic interest:Originally posted by Mike Curwen:
And if you can believe it, I have *one* last question about the mustRefresh() method.
mustRefresh() is designed to prevent the situation that multiple refreshes go on in parallel. It prevents this by setting lastRefresh before the actual refresh happens.mustRefresh() itself *is* synchronized, so only one thread at a time will be able to determine if a refresh is needed. The only way I can see there being multiple refreshes going on in parallel is if I send true into the method [...]
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
I don't like that guy. The tiny ad agrees with me.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|