File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Overloading )  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 )  " Watch "Overloading )  " New topic

Overloading )

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: 3152

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

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

Joined: Sep 20, 2010
Posts: 4616

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.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
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

Joined: Sep 20, 2010
Posts: 4616

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:
subject: Overloading )
jQuery in Action, 3rd edition