6

I have a class containing an ExecutorService that can be shared between threads:

class MyExecutor {
    ExecutorService e = Executors.newSingleThreadExecutor();
    ....
    .... 
    public void add(Runnable r) {
         e.executre(r);
    } 
}

Is it necessary to synchronize the ExecutorService object in the add method since the add method can be called from differens threads or is the ExecutorService thread safe?

Alec
  • 7,110
  • 7
  • 28
  • 53
Rox
  • 2,537
  • 13
  • 47
  • 82

2 Answers2

5

ExecutorService has to use a thread safe queue (Which it does by default). This is all that is needed.

Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106
2

No, there is no need to synchronize calls to add() method.

maximdim
  • 7,785
  • 3
  • 31
  • 45