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
  • 8,529
  • 8
  • 37
  • 63
Rox
  • 2,647
  • 15
  • 50
  • 85
  • 2
    Possible duplicate of http://stackoverflow.com/questions/1702386/is-threadpoolexecutor-thread-safe? – THelper Dec 09 '11 at 13:58

2 Answers2

5

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

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

maximdim
  • 8,041
  • 3
  • 33
  • 48