Re: Multi threading - how small is the smallest 'unit' of code
On 1/3/2011 6:54 AM, David Fernandes wrote:
On Jan 1, 2:41 pm, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
On 1/1/2011 8:13 AM, azserrata wrote:
Say:
//CODE1
(...)
sharedVariable ++;
int i = sharedVariabel;
ITYM `sharedVariable', here and in various other spots.
Yes, of course.
(...)
What problem are you trying to solve? There's probably a way
to solve it without taking so many risks.
Well Eric, I'm just in a learning process and I have to implement a
simple "game" in Java that (I figured) needs multi-threading.
In fact, it's not a very hard problem: I need to implement a "small"
NFA doing some simple stuff along with a interactive data input
process.
I thought to use multi-threading and, while trying to learn how to
implement that in Java, those questions came up.
Okay; I hope your learning goes smoothly and pleasantly. A big
hint for people just starting out in multi-threading, or in parallel
processing of all kinds: Use established patterns. Use them by rote,
if necessary, but use them. Many extremely smart programmers have
written bugs subtle and unsubtle by reasoning "I don't need all that
machinery in this instance BECAUSE ..." and winding up in trouble.
That's the (repeated) genesis of the double-checked locking bug Lew
mentioned upthread, and of many other problems as well.
"Java Concurrency in Practice" has already been mentioned as an
excellent resource, and I second the recommendation. There's also
a good (but brief) section in the on-line tutorial at
<http://download.oracle.com/javase/tutorial/essential/concurrency/index.html>
--
Eric Sosman
esosman@ieee-dot-org.invalid