IntelliJ cannot resolve "newFixedThreadPool" character

I am new to IntelliJ and Java in general. I am trying to learn multithreading and I came across a performer class.

So, I wanted to check this out, 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 the symbol“ newFixedThreadPool. ”I tried“ Invalidate cache and restart ”, but that didn’t help, I tried to synchronize and rebuild the project, but it didn’t work either.

I don’t understand where this problem comes from, because the Executors class is imported. In addition, there was autocompletion of static methods of performers. Maybe the problem is in the import, but if so, how can I fix it?

+6
source share
2 answers

Remove the new keyword on this line:

 execute = new Executors.newFixedThreadPool(6); 

It should be:

 execute = Executors.newFixedThreadPool(6); 

The newFixedThreadPool method is a static method of the Executors class.

+12
source

Remove the new keyword from this line:

 execute = Executors.newFixedThreadPool(6); 

Your syntax is actually trying to call the constructor of the static inner class newFixedThreadPool in the Executor class. This static inner class does not exist. Instead, you need to call the static factory method ...

+1
source

Source: https://habr.com/ru/post/1013036/


All Articles