Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

One thread per object

Thread view: 
oliver.bayer@googlemail.com - 25 Aug 2006 10:32 GMT
Hi Everyone,

I want create for each object that I create in my program an own thread
where this object is running in. But the object itself should not be
derived from the Thread class, e.g.

class bootstrap {
 MyObject obj1 = new MyObject();
 ......
 MyObject objN = new MyObject();

 // Distribute every objX to its own thread
}

The objects itself are created all in a bootstrap class and should than
be distributed to a thread.

Thanks for your comments and suggestions

bilo
Robert Klemme - 25 Aug 2006 10:42 GMT
> Hi Everyone,
>
[quoted text clipped - 14 lines]
>
> Thanks for your comments and suggestions

There's a nice tutorial out there which covers basic things like this:
http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

If you want feedback on the design we need more information.

Kind regards

    robert
Thomas Weidenfeller - 25 Aug 2006 10:59 GMT
> I want create for each object that I create in my program an own thread
> where this object is running in. But the object itself should not be
> derived from the Thread class, e.g.

Implement the Runnable interface (which is anyhow considered to be the
cleaner way to use threads) for your class. Create a separate Thread
object for each Runnable instance of your class.

The Thread API documentation explains all this. Please read it.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

oliver.bayer@googlemail.com - 25 Aug 2006 13:02 GMT
Thomas Weidenfeller schrieb:

> > I want create for each object that I create in my program an own thread
> > where this object is running in. But the object itself should not be
[quoted text clipped - 12 lines]
> ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
> http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Thanks for the replay,
but I don't want to implement the Runnable interface. The objects I
describe could be every kind of object. I am looking for something like
a ThreadManager. I try to describe my problem more precise.
The scenario is something like that, I create a couple of objects
(which could be any types of objects) in my bootstrap class and after
the objectes are created, I want to distribute every object into its
own thread. But the objects neither derived from Thread nor they
implement the runnable interface.

Thanks.
Gordon Beaton - 25 Aug 2006 14:01 GMT
> but I don't want to implement the Runnable interface. The objects I
> describe could be every kind of object. I am looking for something like
[quoted text clipped - 4 lines]
> own thread. But the objects neither derived from Thread nor they
> implement the runnable interface.

It sounds like you want to create a Thread pool of some kind.

But you must also realize that your objects must implement *some* kind
of common interface. It isn't meaningful to pass an arbitrary object
to a Thread where it will "run", since not all objects "do" anything
(consider what should happen if you pass, say, a String or a
BigInteger to a Thread).

To "do" something, the object must provide a method that can be
invoked in the Thread. Runnable is made just for that purpose.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

blmblm@myrealbox.com - 26 Aug 2006 13:42 GMT
> > but I don't want to implement the Runnable interface. The objects I
> > describe could be every kind of object. I am looking for something like
[quoted text clipped - 6 lines]
>
> It sounds like you want to create a Thread pool of some kind.

If that's the goal, be advised that you don't have to write your own
code to create and manage a thread pool; there are library classes
for that, as described in the tutorial previously mentioned, at

http://java.sun.com/docs/books/tutorial/essential/concurrency/pools.html

> But you must also realize that your objects must implement *some* kind
> of common interface. It isn't meaningful to pass an arbitrary object
[quoted text clipped - 4 lines]
> To "do" something, the object must provide a method that can be
> invoked in the Thread. Runnable is made just for that purpose.

Another concurring voice here.  (I keep wondering what the OP has in
mind, since Runnable -- or the newer Callable -- seems so obviously
suitable, but he says no.)

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

Thomas Weidenfeller - 25 Aug 2006 14:25 GMT
> but I don't want to implement the Runnable interface.

If you don't implement something, it will not work. The thread must have
an entry point into your code. Runnable provides that entry point. It
won't get much smaller (you could add a wrapper forwarding from run() tp
some differently named method in your objects, but you still have to
code the wrapper and wrap your objects).

And it doesn't matter much what you want, all that matters is what java
provides you, and what it doesn't.

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Mark Space - 26 Aug 2006 01:03 GMT
> Thomas Weidenfeller schrieb:
>> Implement the Runnable interface (which is anyhow considered to be the
>> cleaner way to use threads) for your class. Create a separate Thread
>> object for each Runnable instance of your class.

> but I don't want to implement the Runnable interface. The objects I
> describe could be every kind of object. I am looking for something like
> a ThreadManager. I try to describe my problem more precise.

Consider some other design pattern beside inheritance.  You could
encapsulate the the objects you want to run with aggregation, or
decorate them, or any number of things.  The easiest might be just to
make a map with one object to run, and another Thread object that is
executing the object.
Patricia Shanahan - 26 Aug 2006 03:11 GMT
>> Thomas Weidenfeller schrieb:
>>> Implement the Runnable interface (which is anyhow considered to be the
[quoted text clipped - 10 lines]
> make a map with one object to run, and another Thread object that is
> executing the object.

You still need some specification of what it means to "run" an object
that does not implement Runnable.

Patricia
Mark Space - 26 Aug 2006 18:23 GMT
>>> Thomas Weidenfeller schrieb:
>>>> Implement the Runnable interface (which is anyhow considered to be the
[quoted text clipped - 15 lines]
>
> Patricia

Right-o.  I assume the OP has some scheme in mind, he just hasn't given
us enough info to guess at what.

I'd use Object, and pair them up with a Thread or a Runable obeject.
When the Object is passed in to be run, I'd use some of the reflection
object to pass in with the Object, as a separate parameter, what method
exactly is to be run.  With a little more reflection, the method could
be inspect and it's parameters could be determined, then take a SWAG at
what parameters should be at runtime.

That's the most general, basic thing I can think of.  One Object to be
run, and the method to execute as a passed parameter.

Obviously some kind of formal Java Interface would be much better, but
the OP may be laboring under some heavy restrictions imposed by the
customer, and may not be able to refactor existing code.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.