wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Overloading Thread.run( )  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Overloading Thread.run( )  " Watch "Overloading Thread.run( )  " New topic
Author

Overloading Thread.run( )

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I tried overloading run() inside an implementer of Runnable. But how do i make a thread call an overloaded version of run() ? One way would be to call the overloaded version from within run() OR create another Runnable implementer . Any other ways ? Is this overloading of any use in real-world apps? . Can i put this in the Threads sub-forum ?

Here is the code that uses Runnable :



By extending Thread class :
Both versions of thread run here. I want to run only the String version. How to do it ?






SCJP 6. Learning more now.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Invoking the start method will spawn a new Thread and it will in turn invoke the run() method. If you invoke the run() or an overloaded version of run() - Its just like invoking anyother method in the instance. If you want to invoke the overloaded version of run() i.e the run(String) you can do it in the run() method body.

And you just cant run the overloaded, because Thread class is designed in such a way that it will invoke the run() method by default. So I dont think you can make it to invoke the run(String).

By the way I like the way you have been trying out different concepts in code.


Mohamed Sanaulla | My Blog
manishh kumar
Greenhorn

Joined: Apr 27, 2011
Posts: 1
But if in the code if you comment r1.start();
and call only r1.run("bingo"); then it should call the overloaded method only.
correct me if i am wrong.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3595
    
  14

Yes, but it won't be run in a separate thread. If you want to do this, you should create a Runnable subclass, with a constructor that takes the parameters you actually wanted to pass to the run method, and store the arguments in some fields. Then the run method can operate on those fields.
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
I was checking the forum to check the solutions for this excercise. I got the expected result but is there any need to make some fields final as given above? I am quite new to Threads so please help me out. Is this solution ok?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3595
    
  14

No, sadly this solution is not fine. First of all, you're extending Thread. Never extend Thread. Implement Runnable or Callable instead.

Secondly, you're using static fields to hold data only one specific instance is supposed to work on. Don't make it static.

You're also using StringBuffer. Don't use StringBuffer, use a String or a StringBuilder. In this case, it doesn't seem like you have any reason to use a StringBuilder, so just use String.

You are also synchronizing on a non-final field. The value of this.b can change while one thread is still executing inside the synchronized block. This effectively makes your synchronized block useless, because another thread can just enter the block after changing the value of this.b.

Actually, if you didn't use static fields, you wouldn't need a synchronized block in the first place, because your instances wouldn't be sharing data.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading Thread.run( )
 
Similar Threads
without giving notify control comes back to print main
threads and uncaught exceptions
synchronized threads
Thread code produces different output