File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Threading

 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program and i want to have multiple threads, but i want to create
them in the same class, can i do this? or do i have to make a class that implements runnable and just create new instances of that in my current class?

Justin Fox
 
Leandro Melo
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I'm not 100% sure about what you want, but let me take a shot. You need to create your "threads". To do that, you can either implement the runnable interface (as you said) or you can extend the Thread class. If your class (suppose it's called MyClass) already extends Thread, you can just instantiate as many MyClass's as you want from inside of it (is that what you mean by "the same class")?

Does that help at all?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
...or do i have to make a class that implements runnable and just create new instances of that ...

Note that when you create an instance of Runnable, you still don't have a thread. You pass the Runnable to Thread's constructor when creating a new Thread.

But you can use the same instance of Runnable to create multiple Thread instances, which might be what you're after here...

MyRunnable runner = new MyRunnable();
Thread t1 = new Thread(runner);
Thread t2 = new Thread(runner);
Thread t3 = new Thread(runner);
etc.
[ April 26, 2007: Message edited by: marc weber ]
 
Sidd Kulk
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i understand from the above discussion is that you want an instance of your class to operate under a multi threaded environment, and you are confused as to whether you need a separate class to do so. If that's it, then you don't necessarily need it. Your class can implement Runnable or extend Thread and, well, Vroom, your code works....

Njoy!!
Sid
[ April 27, 2007: Message edited by: Sidd Cool ]
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so say i have a class..



is this a valid template?
or is it A.start()?

Justin Fox
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You show your constructor making another new A. What's going to happen in that constructor? Do you see a potential problem?

Yes, you need to look into start(). You don't write start, but you call it on the Thread. start() does some deep magic, gets a new thread running and finally calls run(). As you did in your template, that's where you do the real work.

So let's start a thread with this instance of A, not a new one:

There may be some risk here that the new thread will start A running before it is fully initialized by the constructor. That could be bad.

This doesn't feel like a good design. SomeOtherClass would create an instance of A and it would be off and running on another Thread before SomeOtherClass knows what's happening. Seems wrong. Just creating an object shouldn't be enough to make so many other things happen.

Why are you so interested in not using any other classes? Is it important to avoid the typical:

new Thread( new A( args... ) ).start();
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic