IntelliJ cannot resolve symbol "newFixedThreadPool" [duplicate]
Asked Answered
S

2

8

I'm new to IntelliJ and Java in general. I'm trying to learn multithreading and I came across the Executors class.

So I wanted test this, here is a sample of my code.

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class LegController {
    private List<Runnable> legs;
    private ExecutorService execute;

    public LegController() {
        legs = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            legs.add(LegFactory.getLeg("LEFT"));
        }

        execute = new Executors.newFixedThreadPool(6);
    }

    public void start(){

        //TODO
    }
}

But I get an error : "Cannot resolve symbol 'newFixedThreadPool'". I tried "Invalidate cache and restart" but it didn't help, I tried synchronise and rebuild project, but it didn't work either.

I don't understand where this problem is coming from because the class Executors is imported. Besides, there was autocompletion for the static methods of Executors. Maybe there is a problem in the importation, but if so, how could I fix it ?

Sphagnum answered 9/12, 2016 at 13:23 Comment(2)
I'd guess you want execute = Executors.newFixedThreadPool(6);, i.e. without the new keyword, which makes the compiler expect a constructor call (and don't call a constructor).Mountaintop
also there is no Executors.newSingleThreadExecutor(int) method.Dawson
G
27

Remove the keyword new in this line:

execute = new Executors.newFixedThreadPool(6);

It should be:

execute = Executors.newFixedThreadPool(6);

The method newFixedThreadPool is a static method of class Executors.

Greenhorn answered 9/12, 2016 at 13:26 Comment(1)
Thanks a lot, in fact I did put the keyword new by reflex and thus didn't noticed it.Sphagnum
F
1

Remove the new keyword from this line:

execute = Executors.newFixedThreadPool(6);

Your syntax actually tries to call the constructor of a static inner class 'newFixedThreadPool' within the Executor class. That static inner class does not exist. Instead, you have to call a static factory method...

Fiddling answered 9/12, 2016 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.