On Thu, Apr 29, 2010 at 16:38, Kevin Wright
<[email protected]> wrote:
> In a nutshell:
> I have a (mutable) list of customers, ordered by surname
> In thread A, I take that list and display it on a webpage
> In thread B, I take that same list, sort it by reverse order of when I last
> phoned them, and display on a different page
> Because of the way concurrency works, thread A is interrupted by thread B
> halfway through displaying the list, needless to say the output is not as
> expected.
> So it would have been much safer if the list was immutable and thread B
> created a copy of it that was sorted differently.

I understand. And I understand the need of separate lists. But
assuming a modifiable list I could create two lists (both mutable by
their class definition) and pass each list to the propriate thread.
Mutability has the advantage that I could either add additional
elements (from another query to another repository for instance) on
the fly within the business logic.

OK, the language feature could save me from copying the list myself
that is an advantage. But just because I can create mutable objects
doesn't mean I can't handle those in a thread save manner (if it is
simply by providing another copy of the list). In the DB case usually
you anyway have one connection and resultset per thread isn't it?


> Could locks have solved it?  Probably, assuming your concurrent programming
> skills are flawless and you've never encountered a deadlock or race
> condition in your life, and it's also a pretty steep performance cost once
> you've scaled up to 100s of threads.

This would have been onther approach (locking instead of providing
multiple copies of the same result). There might be circumstances
where I definitely want another thread to see the changes from a
different one (e.g. record update in CRM providing new phone number).
-- 
Martin Wildam

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to