简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM