File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Multiple Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multiple Threading" Watch "Multiple Threading" New topic

Multiple Threading

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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

You down with OOP? Yeah you know me!
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401

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?

Leandro Melo
SCJP 1.4, SCWCD 1.4
marc weber

Joined: Aug 31, 2004
Posts: 11343

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);
[ April 26, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
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....

[ April 27, 2007: Message edited by: Sidd Cool ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
so say i have a class..

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

Justin Fox
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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();

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Multiple Threading
It's not a secret anymore!