• Post Reply Bookmark Topic Watch Topic
  • New Topic

Asynchronous submits to a ThreadPoolExecutor

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 22635
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Henry Wong
author
Sheriff
Posts: 22635
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!