• Post Reply Bookmark Topic Watch Topic
  • New Topic

run() method  RSS feed

 
Nitin Kulkarni
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Since it is possible to overload the run() method in a Thread subclass, how would the run(String s) method be invoked from the main method? What is the correct syntax to call it?

class MyThread extends Thread {
public void run() {
System.out.println("running myThread");
}
public void run(String s) {
System.out.println("String in run is " +s);
}
public static void main (String [] arg) {
MyThread t = new MyThread();
Thread y = new Thread(t);
y.start();
}
}

Thanks
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

it is possible to overload Thread#run() but the overloaded method will not be called from Thread#start().

Stefan
 
Nitin Kulkarni
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan,

Can you give an example as to how the public void run(String s) method would be called?

Thanks

Nitin
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(please use code tags!)


I don't know why you ask about 'String s' and pass as Value 'MyThread t', and then call 'new Thread (t)'.
Confused?
 
Nitin Kulkarni
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan W.

I made an error in displaying my code earlier.
It should read as follows.


and say that the overloaded run(String s) can be called but do not show an example of it.

Thanks

Nitin

[ August 15, 2004: Message edited by: Nitin Kulkarni ]
[ August 15, 2004: Message edited by: Nitin Kulkarni ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one old hint:
please use code tags!
It makes code much more readable.
You may edit your post by hitting the edit-icon on your post.
There you surround your code with tags (as seen on tv).
Hit the 'code'-Button!
It's much fun!
Hit it again!

I can't explain the bad example from that book, because I didn't write it - I didn't even read it .

You may call the run (s) - Method as every Method:
From inside MyThread1 with:


But there isn't much sense in it, beside to confuse somebody.

When you call myThread.start (), run () is called automatically, but not run (String s), run (JFrame jf) or run (boolean);

So why should someone name a method 'run', when it behaves very differently?
Perhaps the book explains, what this code want's to show.
Else: burn it.
 
J Kneeland
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand what you are trying to do, you can just do it in the constructor.



Now call it like this:

 
Nitin Kulkarni
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Kneeland,

My question was answered by Stefan W. The following code shows what I am trying to do. The t.run("foo") method calls the overloaded run(String s) method.



Interestingly the output is
String in run is foo
running myThread
[ August 16, 2004: Message edited by: Nitin Kulkarni ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!