[英]In-method inner class vs class level inner class
I was writing something like this : 我在写这样的东西:
class Root {
public void action() {
List<Foo> param1;
List<Foo> param2;
class Node implements Runnable {
public void run() {
// read param1, some stuff, insert in param2
}
}
tpe ThreadPoolExecutor = new ThreadPoolExecutor(..);
// And submit some work, a few times
tpe.submit(new Node());
tpe.submit(new Node());
// some stuff with param1 & 2
}
}
And then I was wondering is there is no performance issue with this. 然后我想知道这是否没有性能问题。 If the fact that I declare the inner class within a method, and use a local variable would not impact performance, maybe the JIT compilator would never be able to optimize the execution of the inner class because of the context around it. 如果我在方法中声明内部类并使用局部变量的事实不会影响性能,则JIT编译器可能由于内部上下文的关系而永远无法优化内部类的执行。 So I wrote something like this, which do exactly the same thing : 所以我写了这样的东西,它们做的完全一样:
class Root {
class Node implements Runnable {
List<Foo> param1;
List<Foo> param2;
public Node(List<Foo> param1, List<Foo> param2) {
this.param1 = param1;
this.param2 = param2;
}
public void run() {
// read param1, some stuff, insert in param2
}
}
public void action() {
List<Foo> param1;
List<Foo> param2;
tpe ThreadPoolExecutor = new ThreadPoolExecutor(..);
// And submit some work, a few times
tpe.submit(new Node(param1, param2));
tpe.submit(new Node(param1, param2));
}
}
The app is under heavy load, so I was wondering the best way to do it from a performance point. 该应用程序负载沉重,因此我想知道从性能角度来看最好的方法。 Can anyone provide an insight ? 任何人都可以提供见解?
The class declaration happens at compile-time, not at run-time. 类声明发生在编译时,而不是在运行时。 There is no run-time performance difference. 没有运行时性能差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.