Re: Splitting processor performance for multiple processes in Java



Lew wrote:
Mark Space wrote:
class MyTask implements Callable<MyResult> {
public MyResult call() throws Exception {
MyResult result = null;
// do stuff, then:
return result;
}
}

A nit: initializing a local variable to 'null' is not always optimal, though it is sometimes necessary.


Since I did compile this example, I just did that to get rid of the compiler warning. I used a local variable because, for someone unfamiliar with Callable, I thought declaring a local variable, then on the next line putting the "do stuff" comment, and then finally returning the local was less confusing than say:

class MyTask implements Callable<MyResult> {
public MyResult call() throws Exception {
// do stuff, then:
return new MyResult();
}
}

Which is cleaner but perhaps less clear how the result should be derived.

Another nit: "throws Exception" got added by the auto-code generation. In this example it's of course not needed.
.



Relevant Pages