Hi guys, I am really really confused with thread and I would really appreciate it if you can help me. Here are my problems:
There are 2 ways to create thread - one extending Thread class and another one is implementing the Runnable interface.
Why many thread does the program has if you don't create any thread using these methods? I've read it from some website that when you run a program JVM auto creates a thread. So, even if you run a very simple program like HelloWorld, it will have at least have one thread? If it is true, does it counted as a thread is you use activeCount() method to get the number of active threads? In one of the program I wrote, the activeCount() returns 7threads even though I created only 2.
You need an object to create a thread right? Why do you need to create an object of the current class to run? I mean in extending methods
Code:
public class myThread extends Thread
{
pubilc void run()
{
System.out.println ("thread running");
}
public static void main (String[] args)
{
//why this line works
myThread yes = new myThread();
yes.start()
//and why this doesn't?
Thread no = new Thread;
no.start();
}
}
Also in Runnable interface
Code:
public class myThread implements Runnable
{
pubilc void run()
{
System.out.println ("thread running");
}
public static void main (String[] args)
{
//what is the difference between this
myThread yes = new myThread();
Thread t = new Thread (yes);
t.start()
//and this? 'this' refer to the object itself right?
//In this case I haven't create any object of myThread class how come it works?
Thread t = new Thread(this);
t.start();
}
}
Thank you in advance. :)
03-11-2006, 03:44 PM
cookie
please, anyone?
03-11-2006, 04:50 PM
Phaelax
//and why this doesn't?
Thread no = new Thread;
no.start();
Cause you forgot the parenthesis on the "new Thread()" part.
//In this case I haven't create any object of myThread class how come it works?
Thread t = new Thread(this);
That actually won't work because you can't use "this" inside a static method, such as main(). Because an object is not created when you call a static method, "this" has nothing to refer to.
Now you could do this instead:
Code:
public class MyThread implements Runnable
{
private Thread thread = null;
public MyThread()
{
thread = new Thread(this);
}
public void run()
{
System.out.println ("thread running");
}
public static void main (String[] args)
{
MyThread thread = new MyThread();
}
}
This works because in your main() method, you have created an object and "this" will refer to that instance of the object.
I hope this makes sense, I'm not to good at explaining "this" stuff.
03-13-2006, 10:56 AM
cookie
Thanks for answering my questions Phaelax. I just compiled the new code with parenthesis and it compiled but it did not create any thread. It did not print out "thread running".
Code:
public class myThread extends Thread
{
public void run()
{
System.out.println ("thread running");
}
public static void main (String[] args)
{
Thread no = new Thread();
no.start();
}
}
And
Code:
public class myThread implements Runnable
{
private Thread thread = null;
public myThread()
{
thread = new Thread(this);
}
public void run()
{
System.out.println ("thread running");
}
public static void main (String[] args)
{
myThread thread = new myThread();
thread.start();
}
}
generated error that says
"cannot resolve symbol
symbol : method start ()
location: class myThread
thread.start();"
03-13-2006, 12:05 PM
Phaelax
First example prints nothing because that instance of run() is never called. You've made a Thread object, not a MyThread object. If you really wanted to do it that way, it'd have to be like this:
Code:
Thread no = new Thread(new MyThread());
start() doesn't exist in the second class because we've emplemented the interface, which only defines methods. Runnable only defines run(), which we have but no where in the class is there a start() method. In the first example, start() exists because it belongs to the super class "Thread" that we've extended and therefore it gets called by default since it hasn't been overridden.
And its good coding practice to start capitalizing your class names. MyThread instead of myThread
03-15-2006, 04:41 AM
cookie
Thanks for the reply guys, I think I understand how thread works now.
03-15-2006, 07:06 AM
destin
You can also do something like:
Code:
new Thread(new Runnable() {
public void run() {
System.out.println("run()");
}
}).start();