Is Executor class thread-safe

R

Rakesh

I am working on a tree traversal algorithm ( reduced form of the main
problem ) - such that we need to figure out the different paths from
the root of the tree to the leaves of the same.

Assuming a BFS algorithm to begin with - I was thinking along the
following lines..

1) Initialize a global blockingQueue..
2) For each child
spawn a new thread for each child.
add child to the current path ..
repeat the algorithm until leaf is reached.
3) If node is a leaf - consolidate the list of nodes visited
(accumulated throughout the thread).
Put it in the blockingQueue to indicate the result.


For each and every step - whenever I need to spawn a new thread , I
wanted to do it under the control of an executor such that -
executor.executor(runnable).

My Q is - (as per the algo above) - clearly multiple threads would be
using the same executor to launch new sub threads. Is the executor
thread-safe with multiple threads calling execute method (with
different instances of Runnable ) of the same Executor reference.
 
K

Knute Johnson

Rakesh said:
I am working on a tree traversal algorithm ( reduced form of the main
problem ) - such that we need to figure out the different paths from
the root of the tree to the leaves of the same.

Assuming a BFS algorithm to begin with - I was thinking along the
following lines..

1) Initialize a global blockingQueue..
2) For each child
spawn a new thread for each child.
add child to the current path ..
repeat the algorithm until leaf is reached.
3) If node is a leaf - consolidate the list of nodes visited
(accumulated throughout the thread).
Put it in the blockingQueue to indicate the result.


For each and every step - whenever I need to spawn a new thread , I
wanted to do it under the control of an executor such that -
executor.executor(runnable).

My Q is - (as per the algo above) - clearly multiple threads would be
using the same executor to launch new sub threads. Is the executor
thread-safe with multiple threads calling execute method (with
different instances of Runnable ) of the same Executor reference.

I think the answer to your question is yes but that's because I think I
know what you are asking :). You don't know what thread may be used to
run your task, it could be the calling thread, a thread in a pool or a
new thread. In any case calling execute() from different threads should
not cause any problem. The blocking queues are thread safe as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top