2

Possible Duplicate:
java thread - run() and start() methods

I made a program which uses threading---

public class ThreadTest{    
    public static void main(String[] args){     
        MyThread newthread=new MyThread();
        Thread t=new Thread(newthread);
        t.start();
        for(int x=0;x<10; x++){
            System.out.println("Main"+x)
        }
    }
} 

class MyThread implements Runnable{
    public void run(){
        for(int x=0; x<10; x++){
            System.out.println("Thread"+x);
        }
    }
}

Now my question is that ... why do we use the "Thread" class and create it's object and pass the "MyThread" calls in it's constructor? can't we call the run method of the "MyThread" object by creating it's object and calling the run method?

( i.e MyThread newthread=new MyThread(); and then newthread.run(); )

What's the reason for creating tread objects and passing the MyThread class in it?

Community
  • 1
  • 1

2 Answers2

10

The MyThread class is not a thread. It is an ordinary class that implements Runnable and has a method called run.

If you call the run method directly it will run the code on the current thread, not on a new thread.


To start a new thread you create a new instead of the Thread class, give it an object that implements Runnable, and then call the start method on the thread object. When the thread starts, it will call the run method on your object for you.

Another way to start a thread is to subclass Thread and override its run method. Again to start it you must instantiate it and call the start method, not the run method.The reason is the same: if you call run directly it will run the method in the current thread.

See Defining and Starting a Thread for more information about starting new threads in Java.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • What i want to know is... why would i want to create multiple threads instead of just i thread i.e Main thread ? – Iam APseudo-Intellectual Dec 03 '11 at 18:15
  • @IamA are you saying that Java does not need the ability to create multiple threads? – Matt Ball Dec 03 '11 at 18:18
  • @IamAPseudo-Intellectual: If you want to do more than one thing at the same time you can use multiple threads. If you use only one thread, when you call `run` your thread cannot do anything else until the method completes. This isn't a problem with your toy example since your method only a fraction of a second to run. But if the method could take several minutes to run it would cause your program to hang and be unresponsive to user input. – Mark Byers Dec 03 '11 at 18:20
  • @MarkByers- Thanks... got it .. multi threading :) – Iam APseudo-Intellectual Dec 03 '11 at 18:23
  • @ MДΓΓ БДLL- I'm new to programming.. sorry if i sound like a Novice.. – Iam APseudo-Intellectual Dec 03 '11 at 18:24
0

start() ->

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. From http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html

For me, only JVM know when give some memory of your program to thread. So we have a standart to use something like this:

new Thread().start();
Michał Skóra
  • 351
  • 2
  • 6
  • 18