Re: My (lack of )wisdom about threads
Stefan Ram wrote:
So, inspired by ?Swing application architecture question.?, here is
my (lack of )wisdoms about threads:
- To correctly use multi-threading, a special education is needed.
I do not yet have taken the time to undergo this, so I need
to refrain from using threads (that is, more than one thread).
I wouldn't say that special education is needed at Java's level of
abstraction. I've been able to do it to some degree, and I certainly
never had it yet. If you know what synchronized does and when you might
want to use volatile, you'll probably be fine. Java 5 and 6 (and
possibly 7) introduce new concurrency utilities that eases some use,
especially if you have a lot of threads interacting.
Recently, I read something I liked very much. So, whenever I
would start to do own inventions in multi-threading, my first
step would be to follow this advice:
?As much as possible, prefer to keep each thread's data
isolated (unshared), and let threads instead communicate
via asynchronous messages that pass copies of data.?
http://www.ddj.com/go-parallel/article/showArticle.jhtml?articleID=215900465
Does anyone else like this advice?
Yes.
But I wonder whether a communication between a view and a
model that live in different threads can also be made robust
by basing this communication on such messages.
Another way of looking it is that such communication would form an event
system.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth