Re: concurrency, threads and objects
On 15.11.2006 12:50, Tom Forsmo wrote:
Hi
I have recently done some thread programming in java, my previous
experience is from posix threads in C. There is one thing that puzzles
me about thread programming in java.
In C there are no function instances (as in objects or similar things),
only function invocations. When programming threads in C there is one
function and several threads with its separate function invocations.
In java you can either create an object and have a number of threads
execute its run() method or you can create one object per thread.
What puzzles me is that in a way both ways seems slightly wrong.
Both are valid approaches although the one instance per thread seems
more common.
Creating a number of objects with a thread for each is sort of like
creating many separate programs/processes, it seems like waste of
objects to start with. Why create as many objects as you would create
threads?
In order to not let the threads interfere with each other. If you work
with Runnable then this is the typical setup, i.e. you create one
Runnable instance per thread. That's not really a waste of objects
since it's just this one instance. An object is nothing "heavy" at
least not by default. Creating an instance without any state is very
cheap on modern JVM's. You can easily create tons of objects per second.
Creating only one object and creating a number of threads for that
objects run method, also seems wrong, for two reasons. 1) the object
could then not have any state unless it was to be shared
Exactly. And you must synchronize access to that state. But if the
Runnable just implements some kind of function (i.e. has no state of its
own) it is perfectly ok to execute it from multiple threads.
> and 2) when
reading the name of the thread all threads are named the same.
This is wrong. The name is read from the Thread instance not from the
Runnable the thread executes (unless of course you make the name you are
referring to a member of that Runnable).
I understand that it would be normal to create other objects and execute
their methods run(), which would then effectively create a
function/method invocation and it all aligns well with my previous
perception. But the startup part of it seems a bit strange to me.
You do not actually create a function but an object. That object can
have methods (and typically has). You can invoke methods on an object
from multiple threads - in some cases it works, in others it does not.
That completely depends on the class implementation: if there is no
state or if access to state is properly synchronized then it is likely
to work from multiple threads - if there is state and accesses are not
properly synchronized all bets are off.
Anyone care to help me push my perception into alignment again.
It seems to me that your problem might more lie in the area of object
oriented thinking. This can be difficult when coming from a procedural
background. The same happened to me when I embraced OO. It can take
some time to get used to it. However, there are plenty resources out
there that introduce OO.
For reading up on Java threads I can very much recommend Doug Lea's book
and website:
http://www.awprofessional.com/bookstore/product.asp?isbn=0201310090&rl=1
http://g.oswego.edu/dl/
Kind regards
robert