Multiple threads...how?
Hi i am very new to threads and i want to know is it possible to have more than one thread running in a class and if so how do i do it?
at the moment i have one thread called blah:
public GUI()
{
// some other stuff here.....
blah =new Thread(this);
blah.start();
}
publicvoid run()
{
//some stuff here...
}
that works fine but i want to add another thread but obviously i cant make another method called run() so how do i do it?
im sorry if this is a very nooby question, any help is welcome
thanks
Here's a little demo:
class ManyThreads {
public ManyThreads() {
Thread blah = new Thread() {
public void run() {
for( int i = 0; i < 10; i++ )
System.out.println("i = " + i);
}
};
Thread blah2 = new Thread() {
public void run() {
for( int j = 0; j < 10; j++ )
System.out.println("j = " + j);
}
};
blah.run();
blah2.run();
}
public static void main(String[] argv) {
new ManyThreads();
}
}
Threads don't run in a class.
If you want to do something else in another thread, you create another class that implements Runnable and create a new Thread for an instance of that Runnable.
Where you start the thread(s) doesn't matter, it doesn't have to be in the same class. Thread anotherThread = new Thread(new MyRunnable());
anotherThread.start();
ok i think i understand, thanks both of you
Navy_Code's code doesn't actually create any new threads. Threads should be started using the start() method.
Additionally, extending Thread (which Navy_Code is doing by creating an anonymous subclass of Thread) isn't right from an OO point of view. "Stuff that needs to be run on a thread" should be implemented as a Runnable.
Here's another demonstration: (to touch on Herko's remarks)
class MyThread extends Thread {
public void run() {
for( int i = 0; i < 10; i++ ) System.out.println("i = " + i);
}
}
class AnotherThread implements Runnable {
public void run() {
for( int j = 0; j < 10; j++ ) System.out.println("j = " + j);
}
}
class TestClass {
private static MyThread mt;
private static AnotherThread at;
public static void main(String[] argv) {
mt = new MyThread();
at = new AnotherThread();
mt.start();
at.run();
}
}
Close, but no cigar.
You now properly start MyThread using the start() method, but calling AnotherThread.run() doesn't create another thread, it just executes AnotherThread.run() on the current (main) thread.
For AnotherThread to run on its own thread, you should have said this:at = new AnotherThread();
Thread atThread = new Thread(at);
atThread.start();
> Close, but no cigar.
I said , "to touch on", not "to demonstrate the points that Herko made to exaction."
> You now properly start MyThread using the start()
> method, but calling AnotherThread.run() doesn't
> create another thread, it just executes
> AnotherThread.run() on the current (main) thread.
Since you said, "you" I am assuming you were directing that post at me. Since that is the case, I will simply inform you that I am well aware of that fact that calling "run" does not spawn a new thread and will not take it personally that you are telling me what is "proper" and what isn't.
>
> For AnotherThread to run on its own thread, you
> should have said this:at = new
> AnotherThread();
>
> Thread atThread = new Thread(at);
> atThread.start();
Again, I am aware of this. That was not my intention. My intention was to show an example of how a "Runnable" interface is implemented. Simple as that.
No problem with that.
My only intention was to explain to the OP that your code contained several issues which I deemed it prudent to point out so as to prevent him from taking your code for an actual example of how to create multiple threads. This being the "New to Java" forum, I wasn't sure he (or other visitors to this topic) would be able to make that distinction on his own.
With regards to the "proper" remark, I just wanted to point out that extending Thread is not the right thing to do if one is interested in designing an application following the rules of Object Orientation as they are generally understood. Nothing personal intended, so it's good you didn't take it that way.