aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes begin execution of a thread -- start or run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "begin execution of a thread -- start or run?" Watch "begin execution of a thread -- start or run?" New topic
Author

begin execution of a thread -- start or run?

hanning Aha
Greenhorn

Joined: Nov 09, 2002
Posts: 4
I was working on the mindQ's mock exam, and #38
38. What line of code would begin execution of a thread named myThread?
is it myThread.run() or myThread.start()? or both? and why? The answer says it's myThread.start().
Thanks.
Roan Nicolas
Greenhorn

Joined: Nov 11, 2002
Posts: 23
Basically, start() method causes the thread to begin execution. The Java Virtual Machine however calls the run method implemented in the thread.
Hope that makes sense.
[ November 21, 2002: Message edited by: Roan Nicolas ]

Owee<p>SCJP 1.4
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Originally posted by hanning Aha:
I was working on the mindQ's mock exam, and #38
38. What line of code would begin execution of a thread named myThread?
is it myThread.run() or myThread.start()? or both? and why? The answer says it's myThread.start().
Thanks.

hello Aha!
They like to word these questions with all sorts of tricks!
If you have a class that extends Thread you should override the run() method that gets added to your class. You should override it because the run() method that you inherit does nothing and I'm guessing you'll want to do something in the method.
And you would call that run() method with "instanceName.start();"
If you have a class that implements the Runnable interface, it is basically the same - you need to call the run() method with "intanceName.start();"
xxx.start() will result in the run() method you normally override that) being called. The xxx above must be an object that extends Thread Class or an instance of an object that implements the Runnable Interface.
.......
tricky questions can look like:
What is the output of the code below:

Answer is "it's me!" printed out only once.
Because there are no threads involved in this at all - f.start() calls nothing! and f.run() calls the run() method that we defined.
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
examples of classes that EXTEND Thread:
1) we did not override the run() method that we inherit from Thread class:

2) now we will override the run() method so our pet is not so boring!

notice that when we call c.start(), the method run() is started but we return to the main method right away and continue - the Thread scheduler did not garuntee that the code in the run() method would finsih before returning to main.
[ November 21, 2002: Message edited by: david eberhardt ]
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
now let's do this by IMPLEMENTING the RUNNABLE interface:
1) you'll notice 2 things; to create a new Thread object, you have to add a step
2) you MUST implement the run() method inherited from the Runnable Interface!

our next exmple:

note: I inserted calls to the Thread.currentThread() to show how we are running in 2 differnt threads here.
[ November 21, 2002: Message edited by: david eberhardt ]
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
sorry!
here's the second example:
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
aha:
if you look at the API docs, the start() method clearly states

public void start()
Causes this thread to begin execution. The Java Virtual Machine calls the run method of this thread.

simply put the start() method creates a thread and starts it to run/execute.
HTH
/SAmith
hanning Aha
Greenhorn

Joined: Nov 09, 2002
Posts: 4
Thank you very very much, guys. I got it.
Calling run() brunches no new thread. It's still the original main thread doing the things in run() method.
Thanks a lot.
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Originally posted by hanning Aha:
Thank you very very much, guys. I got it.
Calling run() brunches no new thread. It's still the original main thread doing the things in run() method.
Thanks a lot.

if you examine my code samples, I place some println() statements in there to display the Thread info.
In the examples that implement the Runnable interface, you can see the different threads.
If you extend Thread class, I'm not sure you get two distinct threads or not.
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158

in my last post, I spoke too soon!
I ran my Dog extends Threadexample and you can see that the main thread is different that the thread that the run method is in.
That's why I added the calls to "currentThread()" !
it's true
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
There is a difference between invoking the start() method and the run () method.
Does the following code illustrate this?
class MyThread extends Thread {
MyThread(String s) {
super(s);
}
public static void main(java.lang.String[] args) {
MyThread myThread1 = new MyThread("One");
MyThread myThread2 = new MyThread("Two");
System.out.println("I invoke the method start()");
myThread1.start();
myThread2.start();
// System.out.println("I invoke the method run()");
// myThread1.run();
// myThread2.run();
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(this.getName() + " - i = " + i);
}
}
}
Situation 1:
The main method invokes the start () method, and the following output will be produced:
One - i = 0
Two - i = 0
One - i = 1
Two - i = 1
One - i = 2
Two - i = 2
One - i = 3
Two - i = 3
One - i = 4
Two - i = 4
One - i = 5
Two - i = 5
One - i = 6
Two - i = 6
One - i = 7
Two - i = 7
One - i = 8
Two - i = 8
One - i = 9
Two - i = 9
(The threads are running "at the same time".)
Situation 2:
If I change the code, and invoke the run () method, instead of the start () method, as follows:
// System.out.println("I invoke the method start()");
// myThread1.start();
// myThread2.start();
System.out.println("I invoke the method run()");
myThread1.run();
myThread2.run();
The following output will be produced:
I invoke the method run()
One - i = 0
One - i = 1
One - i = 2
One - i = 3
One - i = 4
One - i = 5
One - i = 6
One - i = 7
One - i = 8
One - i = 9
Two - i = 0
Two - i = 1
Two - i = 2
Two - i = 3
Two - i = 4
Two - i = 5
Two - i = 6
Two - i = 7
Two - i = 8
Two - i = 9
Can I draw the conclusion that the run () method will not run the threads "at the same time"?


I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: begin execution of a thread -- start or run?