Is Executor class thread-safe

Discussion in 'Java' started by Rakesh, Oct 13, 2008.

  1. Rakesh

    Rakesh Guest

    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.
     
    Rakesh, Oct 13, 2008
    #1
    1. Advertising

  2. Rakesh wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Oct 13, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal

    Cancelling an Executor thread

    iksrazal, Jun 21, 2004, in forum: Java
    Replies:
    0
    Views:
    792
    iksrazal
    Jun 21, 2004
  2. Kenneth P. Turvey

    Executor

    Kenneth P. Turvey, Sep 19, 2005, in forum: Java
    Replies:
    8
    Views:
    850
    Kenneth P. Turvey
    Sep 20, 2005
  3. Prafulla T
    Replies:
    5
    Views:
    566
    Chris Uppal
    Feb 7, 2007
  4. Gabriel Rossetti
    Replies:
    0
    Views:
    1,325
    Gabriel Rossetti
    Aug 29, 2008
  5. John Nagle
    Replies:
    5
    Views:
    473
    John Nagle
    Mar 12, 2012
Loading...

Share This Page