Forums Register Login

Asynchronous submits to a ThreadPoolExecutor

+Pie Number of slices to send: Send
Please take a look at the following code snippet. A threadpoolExecutor object is created with the following two lines of code.

private final ExecutorService myThreadPool;
this.myThreadPool = Executors.newCachedThreadPool();


Now there are a bunch of asynchronous threads that have reference to the myThreadPool object. They might be invoking
myThreadPool.submit(Runnable Task) asynchronously

Q 1.) Is myThreadPool thread safe ?.

Q 2.) If I want to synchronize what is the best way to synchronize this with minimal impact to application performace

Q 3.) If I make the submit task return a Future object

Future <x> = myThreadPool.submit(Runnable Task);

Will the performance be slow ?

Thanks Much
+Pie Number of slices to send: Send
Q 1.) Is myThreadPool thread safe ?.

The threadpool executor is threadsafe. You can submit tasks from different threads. Now, whether the tasks submitted to the threadpool is thread safe is your responsibility.

Q 2.) If I want to synchronize what is the best way to synchronize this with minimal impact to application performace


Synchronize what?!? Synchronization is a tool used to solve threading issues. You don't synchronize if you don't having threading issues.

Q 3.) If I make the submit task return a Future object

Future <x> = myThreadPool.submit(Runnable Task);

Will the performance be slow ?


The submit() methods return a Future object. What do you mean by "make the submit task return a Future object"?

Henry
+Pie Number of slices to send: Send
wow!. I am I talking to the Henry Wong who authored Java Threads ?. Thank you sooo much for your reply.

I did not know Threadpoolexecutor was threadsafe. So even if multiple threads do a submit tasks there should be no issues.

Also please elaborate on how if the Runnable task I submit, not being thread safe might impact this code snippet.

About the question I had on the FutureObject, if a task is submitted to a ThreadPoolExecutor, from my understanding this FutureObject will give the status of the successful completion of this Runnable Object that was being submitted.
The reason for the confusion is that, since the Future Object will only be updated when the Runnable task is executed. But from my understanding in a ThreadPoolExecutor all the Runnable tasks submitted the threadpool may be executed some time in the future, and not immediately.

Thanks a lot for your prompt response!
+Pie Number of slices to send: Send
 

Also please elaborate on how if the Runnable task I submit, not being thread safe might impact this code snippet.



The executor just runs the tasks. If you submit two tasks that share a variable, or shares a variable with an external thread, you still have to make sure that it is thread safe. Just because the threadpool is thread safe doesn't mean that the tasks are thread safe, that is your responsibility.

About the question I had on the FutureObject, if a task is submitted to a ThreadPoolExecutor, from my understanding this FutureObject will give the status of the successful completion of this Runnable Object that was being submitted.



Yes, the future object contains the methods that wull be used to query the status of the task. But there is no reason it should impact performance. It is a lightweight operation just to get the status from the executor.

Henry
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5730 times.
Similar Threads
Stopping a thread in threadPoolExecutor
ExecutorService invokeAll - blocking?
ThreadPoolExecutor restart
cancel tasks after a certain time
ThreadPoolExecutor.DiscardPolicy cannot be resolved
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:26:49.