Using executor on android

Asked

Viewed 424 times

0

I’m trying to use an executor to record data in the database using threads. When instantiating an object from the Executor Service class and retrieving an instance from Executors.newSingleThreadExecutor(); returns the error that is not finding the newSingleThreadExecutor instance/declaration.

   ExecutorService exec = new Executors.newSingleThreadExecutor();

                       exec.execute(new Runnable() {
                            @Override
                            public void run() {

                                //Adicionando tarefa
                                addTask(itemList);

                            }
                        });
  • You added the respective import? import java.util.concurrent.ExecutorService

  • sim -> import java.util.Concurrent.Executorservice; import java.util.Concurrent.Executors; -> only newSingleThreadExecutor() is not recognizing

1 answer

1


Don’t use the new.

Executors.newSingleThreadExecutor() is a method and not a class.

ExecutorService exec = Executors.newSingleThreadExecutor();

Its function is to create an instance of Executorservice which in this case uses a single worker thread.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.