concurrency, threads and objects



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.

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?

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 and 2) when reading the name of the thread all threads are named the same.

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.

Anyone care to help me push my perception into alignment again.

tom
.



Relevant Pages

  • Re: concurrency, threads and objects
    ... 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. ... When programming threads in C there is one function and several threads with its separate function invocations. ...
    (comp.lang.java.programmer)
  • Re: concurrency, threads and objects
    ... 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 java you can either create an object and have a number of threads execute its runmethod or you can create one object per thread. ... But if the Runnable just implements some kind of function it is perfectly ok to execute it from multiple threads. ...
    (comp.lang.java.programmer)