Adam,
It would be helpful if this was in a Jira issue where we could comment,
and submit a patch, so we can tell what changed.
-Adrian
Adam Heath wrote:
So, one year ago I was tasked with fixing some deadlocks in
Webslinger(the website container Brainfood has developed). While
investigating solutions to the problems I was seeing, I stumbled across
the book, "Java Concurrency in Practice".
OH
MY
GOD
!
It's not normal for me to get all ecstatic about a programming book, but
this one has to be at least an 11(on a scale of 1-10). Anyone doing
*any* multi-threaded programming in java absolutely *must* read this book.
Anyways, after reading said book, I completely rewrote the internals of
webslinger, following the guidelines I had read about. It's now been a
year, and what I figured out seems to be running great; no normal code
paths thru webslinger take out *any* locks now.
I've decided to start applying this knowledge against OfBiz. My first
attempt is attached.
While SequenceUtil is not a highly-contended resource, it is simple
enough, and used often enough, so that others can understand what is
going on, and it's possible to see if the change actually breaks anything.
Summarizing, here is how it functions(at a high-level).
1: Any fields that have to be modified together have to be moved to
separate class.
2: Said class is made final, and *all* fields are also made final;
equals() and hashCode() are implemented.
3: The parent class is modified to use an AtomicReference.
4: Any time the parent class needs to change one of the original fields,
it makes a copy of the current reference with the new values, then does
an atomic compareAndSet. This can cause multiple allocations when
contended, however, java 1.6 is smart enough to do some allocations on
the stack, so in general this is not a problem.
These changes invariably increase the size of the code; however, they do
reduce overhead in the long run, so I consider them beneficial.
Attached you will find SequenceUtil.java; I've modified it to be
non-blocking. I've done "ant run-install", with no problems, but have
*not* yet run any test cases.
I'm sending it here(to this list) first, before checking it in, as this
is a radical departure from simple programming practices, and would like
others to discuss if I should do this in other places.