If I use an executor to submit tasks as follows:
public SomeClass{
public void doSomething(){
Runnable r = new Runnable(){
public void run(){
callSomeMethod();
}
}
executor.execute(r);
}
}
Does it make any difference if callSomeMethod is
- a private method of the
Runnable ror - a method of
SomeClassin any way related to concurrency issues?
I am not refering to access of variables.
There are subtle differences, but it should not matter to you.
If callSomeMethod() is private and declared in the enclosing (outer) class, JVM specification would deny the anonymous Runnable access to it (Private methods are only visible to the declaring class. The concept of inner classes is unknown to the VM).
However, by the language specification, this is a valid call. The java compiler does some magic in this case – it generates a synthetic accessor (basically a method not declared in the source, but purely generated by the compiler, to enable access to the private method).
The same happens for class members that are private. So strictly speaking there is a difference, it will enlarge the resulting class file(s) a tiny bit.
You should choose whatever form is most appropiate it terms of code readability. The Runnable can be viewed as part of the method (and thus its containing class), so if callSomeMethod() is private to the class declaring class, it should be declared private.
As to where to best put callSomeMethod(), make that decision based on what its purpose is/what it does (where you would logically expect to find the operation performed by the code).