 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lonelyplanet999 Guest
|
Posted: Sat Mar 06, 2004 7:15 am Post subject: Thread behaviour |
|
|
From textbook, I was told to implement Thread code, the run() method
need to be overriden. However, I just found below code from one java
programmer exam web site that said the reverse.
Below program compiled & ran without error. But I would like to ask if
public void start() was replaced by public void run(), how the
program's behaviour got changed ? Did the difference laid at separate
stacks being created for thread instances 'first' & 'second' if public
void run() implemented instead of public void start() ? From program
execution output I didn't see the difference. :)
public class Holt extends Thread {
private String sThreadName;
public static void main(String argv[]) {
Holt h = new Holt();
h.go();
}
Holt() {}
Holt(String s) {
sThreadName = s;
}
public String getThreadName() {
return sThreadName;
}
public void go() {
Holt first = new Holt("first");
first.start();
Holt second = new Holt("second");
second.start();
}
public void start() {
for (int i=0; i<2; i++) {
System.out.println(getThreadName()+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
|
|
| Back to top |
|
 |
Thorsten Seelend Guest
|
Posted: Sat Mar 06, 2004 9:53 am Post subject: Re: Thread behaviour |
|
|
Am 5 Mar 2004 23:15:04 -0800 schrieb lonelyplanet999
<lonelyplanet999 (AT) my-deja (DOT) com>:
| Quote: | From textbook, I was told to implement Thread code, the run() method
need to be overriden. However, I just found below code from one java
programmer exam web site that said the reverse.
Below program compiled & ran without error. But I would like to ask if
public void start() was replaced by public void run(), how the
program's behaviour got changed ? Did the difference laid at separate
stacks being created for thread instances 'first' & 'second' if public
void run() implemented instead of public void start() ? From program
execution output I didn't see the difference.
...
|
Both are right )
You do have to override the "run()" method but you do NOT directly call it.
Doing so, the method would be executed in the calling thread (main-thread,
GUI-thread) and the calling method has to wait until run() has finished.
Invoking the "start()" method of your thread does exatly what you needs.
It creates a new thread (technical thread, not an instance of the class
Thread),
enables the run() method to be executed "inside" this new thread
and then immediately returns to the calling method.
On the other side you normally should NOT override the start() method
without
exactly knowing what it is expected to do.
Bye
Thorsten Seelend
--
Erstellt mit M2, Operas revolutionärem E-Mail-Modul:
http://www.opera.com/m2/
|
|
| Back to top |
|
 |
Steve Horsley Guest
|
Posted: Sat Mar 06, 2004 1:03 pm Post subject: Re: Thread behaviour |
|
|
lonelyplanet999 wrote:
| Quote: | From textbook, I was told to implement Thread code, the run() method
need to be overriden. However, I just found below code from one java
programmer exam web site that said the reverse.
Below program compiled & ran without error. But I would like to ask if
public void start() was replaced by public void run(), how the
program's behaviour got changed ? Did the difference laid at separate
stacks being created for thread instances 'first' & 'second' if public
void run() implemented instead of public void start() ? From program
execution output I didn't see the difference. :)
public class Holt extends Thread {
private String sThreadName;
public static void main(String argv[]) {
Holt h = new Holt();
h.go();
}
Holt() {}
Holt(String s) {
sThreadName = s;
}
public String getThreadName() {
return sThreadName;
}
public void go() {
Holt first = new Holt("first");
first.start();
Holt second = new Holt("second");
second.start();
}
public void start() {
for (int i=0; i<2; i++) {
System.out.println(getThreadName()+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
|
You got that from a book??? I think you should burn it.
* You should NEVER override start unless somewhere in there you
call super.start(). Oferriding start() as in the example above
leaves an unstarted thread for the duration of the program.
The above program is not multithreaded in any way. The main thread
creates three (yes three) Thread derivatives, does not start any
of them because the proper start() has been overriden.
Instead, the main thread runs through the printing loops itself,
whichis why you see the first loop finish before the second loop
prints anything.
Please stop extending Thread. It is a bad idea.
Steve
|
|
| Back to top |
|
 |
lonelyplanet999 Guest
|
Posted: Sat Mar 06, 2004 3:11 pm Post subject: Re: Thread behaviour |
|
|
Thorsten Seelend <seelend (AT) gmx (DOT) de> wrote
| Quote: |
Both are right )
You do have to override the "run()" method but you do NOT directly call it.
Doing so, the method would be executed in the calling thread (main-thread,
GUI-thread) and the calling method has to wait until run() has finished.
|
You mean if I don't override "run()" but calling "start()" directly,
no new call stack will be created ? If "start()" do some lengthy job
the calling thread (let say the main-thread) has to wait indefinitely
until "start()" completes ?
| Quote: |
Invoking the "start()" method of your thread does exatly what you needs.
It creates a new thread (technical thread, not an instance of the class
Thread),
enables the run() method to be executed "inside" this new thread
and then immediately returns to the calling method
|
If I override "run()" and call "start()" instead of directly calling
"run()", JVM will create a new separate call stack from its calling
thread and start executing the "run()" code ? The sharing of CPU time
(suppose one CPU case) between the calling thread and the newly
created thread being determined by the JVM implementation ? That is,
sometimes I can see the calling thread running while sometimes the
newly created thread running ?
| Quote: |
On the other side you normally should NOT override the start() method
without
exactly knowing what it is expected to do.
|
I will not do that but just curious about the question's
implementation method. Before seeing this question, I don't know
overriding "start()" alone without overriding "run()" is legal.
| Quote: |
Bye
Thorsten Seelend
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|