I have a class organized as follows:
public class MyClass {
ExecutorService pool;
public MyClass(){
pool = ... //inited by a class that implements ExecutorService
}
public final void submit(Runnable run){
pool.execute(run);
}
}
Is the method submit
thread safe or I should use a Lock
-based system? E.g.
ReentrantLock look = new ReentrantLock();
public final void submit(Runnable run){
lock.lock();
try{ pool.execute(run); } finally{lock.unlock();}
}
No you don't need the lock when calling ExecutorService#submit.
Memory consistency effects: Actions in a thread prior to the submission of a Runnable or Callable task to an ExecutorService happen-before any actions taken by that task, which in turn happen-before the result is retrieved via Future.get().
or when calling Executor#execute:
Memory consistency effects: Actions in a thread prior to submitting a Runnable object to an Executor happen-before its execution begins, perhaps in another thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With