Forums Register Login

Passing parameters from parent method to an inline class defined inside the method

+Pie Number of slices to send: Send
Hello All,

Though the below snippet works fine, I have couple of questions to better understand on exactly what is happening behind.

Say I have a code like below



As you have noticed from the above code, parameter "param" is being passed to execute method and in turn that is being used inside run() method of inline anonymous implementation of Runnable.

If we keep inline declaration aside, technically this should have been technically equivalent to



and in ExecutorService


So, if we compare the first and second ExecutorService, are the steps to set the param and making it available with in run() method of and others are happening behind the scenes by compiler. If Yes, how would compiler determine, if that should use a setter or a constructor to make sure the param is available to Runnable instance.


Also, is there any prominence in declaring the param as "final" with respect to this kind of method passing.


Please advise.
+Pie Number of slices to send: Send
your annonymous class declaration looks wrong
should be:
+Pie Number of slices to send: Send
Hi,

These two methods are not equivalent.
Look at this code:


In this code Runnable 1 and Runnable 2 are objects of the class MyRunnable.
Both of them have each own, independent copy of 'param', and can freely modify it (can modify own copies of param, but not param from the execute method ).

Runnable 3 and Runnable 4 are objects of the anonymous Runnable class,
they both use the same param variable from the enclosing class (method).
Runnables 3 and 4 do not compile because param is declared final (final variables cannot be modified).
Param must be declared final, because anonymous classes can only reference local variables and parameters from the enclosing class or method that are declared final.
In other words, anonymous inner classes cannot modify variables and parameters of the enclosing class/method, can only read them.
If you remove instructions that modify param variable, this code compiles fine.
This is a requirement of Java language semantics - final clause makes that "shared" parameter immutable and therefore thread-safe,
so developer don't have to bother with synchronization in inner classes.

+Pie Number of slices to send: Send
 

Randall Twede wrote:your annonymous class declaration looks wrong
should be:



My anonymous class is an implementation of Runnable interface. So it should be declared as new Runnable(){}. Please refer http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm
+Pie Number of slices to send: Send
Thank you Ireneusz.



eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6998 times.
Similar Threads
ThreadPool, don't execute two jobs simultaneously if certain conditions are met
Doubt
Thread sync issue
Which type of implementation, extends Thread or implements Runnable. ?
How to correctly use a fixed size thread pool?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:46:31.