views:

251

answers:

5

in the main method if I write this:

Thread aThread = new Thread();
aThread.run();

what will happen???

+10  A: 

It will run in current thread. You will not start new thread this way.

But that doesn't really matter in your example since you gave new Thread no code to run anyway.

Peter Štibraný
what is current thread here?
Johanna
The main thread that is executing the main() method.
Don Kirkby
+3  A: 

The thread that runs the main() code is the current thread. Creating a Thread object and calling one of its methods (other than start()) is like calling a method of class Integer or String - it doesn't create a new actual thread.

In your code example, execution of the main method will continue only when the run() method has finished running. This means that if the run() method has an endless loop (let's say it's waiting for incoming requests), then the main() method will never continue running, even if there are more lines of code after the call to run().

Calling aThread.start() creates a new actual thread (represented by the object aThread), makes the new thread call the run() method, and returns execution of the original thread to the next line in the main(). This means that the new thread can run around in circles forever, but it doesn't stop the main() code from creating more threads or performing other tasks.

Yuval
+3  A: 

It will just run like you're calling a normal method. So the method will be running in the same thread that calls the method.

nanda
A: 

If you call the start-method on a Thread class, the start-method will return after a while, but in concurrency will run the content of the run-method. If you call the run-method directly it will be executed and return to the caller, after the method is completely done - as every normal method call.

Mnementh
+1  A: 

It will run in the current Thread not in new Thread So If you call run method yourself it is meaningless. Because It doesn't create a new Thread.

Firstthumb