[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Concurrency Questions
Doug
Lea, Concurrent Programming in Java, 2nd ed.
pg6:
-
always lock during updates to object fields
-
always lock during access of possibly updated object fields
-
never lock when invoking methods on other objects
pg
93:
"Access and updates to the memory cells corresponding
to fields of any type except long or double are guaranteed to be atomic. This
includes fields serving as references to other objects. Additionally, atomicity
extends to volatile long and double. ... However as seen below, atomicity alone
does not guarantee that you will get the value most recently written by any
thread. For this reason, atomicity guarantees per se normally have little impact
on concurrent program design."
pg
95:
"The
model also allows inconsistent visibility in the absence of synchronization. For
example, it is possible to obtain a fresh value for one field of an object, but
a stale value for another. Similarly, it is possible to read a fresh, updated
value of a reference variable, but a stale value of one of the fields of the
object now being referenced." !!
THEREFORE...
In #1,
yes, i CAN be 1.
In #2,
you should never get a Dr. Watson. The question is, can the 1st thread get
visibility to the new arraylist before the initialization is complete? With a
little uncertainty on my part, I believe the answer to that is yes, the newlist
reference could leak out before its initialization is
complete.
The
only guarantee for safety in these situations is for thread 2 to get the very
same lock that thread 1 just released.
I
strongly recommend anyone working with threads to read Lea's book and also "The
Java Language Specification", Gosling, Joy, Steele.
regards
Hello,
I
have two questions. Please assume multiprocessor machines with each
thread running on separate cpu's. Also, if you can point me to any
reference that backs your opinion/fact would be great. Am I safe on both
of the following?
1. Lets say I have the following
code:
public class MyTest extends
Thread
{
static int i =
0;
// Please not NO
sync's
run()
{
i++;
}
}
Now
let's say that I spawn 2 of these and they are on different cpu's and they
both execute i++ at the same time. Is there EVER a chance that the
result of i can EVER be 1 or will it always be 2.
2. Now here is another code
snippet:
static ArrayList myStaticList =
null;
public ArrayList getList(boolean
shouldRefresh)
{
if
(shouldRefresh)
{
// Not
only sync on refreshing, not getting
synchronized (someLockObject)
{
ArrayList newList = new ArrayList();
// Code here to get new list from database and fill
newList
myStaticList = newList;
}
}
return
myStaticList;
}
My
question is that let's say that one thread (different cpu) is executing return
myStaticList at the same time as another thread is executing myStaticList =
newList. Is it possible that the return myStaticList will EVER return a
half/limbo state object or worse yet a Dr. Watson or
something?
Help
is wanted and appreciated.
Thanks,
Dale