Java Child Threads

Discussion in 'Java' started by Hugo, Feb 19, 2008.

  1. Hugo

    Hugo Guest

    Here is a conundrum...

    User authenticates to a web application.....Parent thread is created.

    At some point during the user's interaction with the application, I
    would like to create a child thread that can perform a task
    independent of the parent thread. Whether the child thread is
    successful or not should be irrelevant to the parent thread.

    And the execution of the child thread's task should not delay the
    parent thread....i.e. the parent thread should not wait until the
    child thread completes its task.

    Is this possible in Java...any pointers on hows to get this started?
     
    Hugo, Feb 19, 2008
    #1
    1. Advertising

  2. Hugo wrote:
    > Here is a conundrum...
    >
    > User authenticates to a web application.....Parent thread is created.
    >
    > At some point during the user's interaction with the application, I
    > would like to create a child thread that can perform a task
    > independent of the parent thread. Whether the child thread is
    > successful or not should be irrelevant to the parent thread.
    >
    > And the execution of the child thread's task should not delay the
    > parent thread....i.e. the parent thread should not wait until the
    > child thread completes its task.
    >
    > Is this possible in Java...any pointers on hows to get this started?


    That is really what threads are for. There are a million ways to do
    this but the simplest is to wrap the code you want to run in another
    thread in a Runnable and create a thread and start it.

    // parent decides to run a task in another thread
    Runnable r = new Runnable() {
    public void run() {
    // do your thing
    }
    };
    new Thread(r).start();
    // the Runnable above is now running concurrently with the parent
    // parent continues

    --

    Knute Johnson
    email s/nospam/knute/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
     
    Knute Johnson, Feb 19, 2008
    #2
    1. Advertising

  3. Hugo

    Peter Duniho Guest

    Peter Duniho, Feb 19, 2008
    #3
  4. Hugo

    Daniel Pitts Guest

    Hugo wrote:
    > Here is a conundrum...
    >
    > User authenticates to a web application.....Parent thread is created.
    >
    > At some point during the user's interaction with the application, I
    > would like to create a child thread that can perform a task
    > independent of the parent thread. Whether the child thread is
    > successful or not should be irrelevant to the parent thread.
    >
    > And the execution of the child thread's task should not delay the
    > parent thread....i.e. the parent thread should not wait until the
    > child thread completes its task.
    >
    > Is this possible in Java...any pointers on hows to get this started?

    Its quite possible, even easy.

    One thing to consider though, is what happens if you get "slammed" by
    traffic. Often times web containers will pool their main threads and
    block incoming connections until a thread becomes available. If you are
    spinning off these child threads without any consideration, you may
    overload the JVM with threads. If it is a small tool used internally to
    your company, don't worry too much about it. If it is a webapp that
    serves a high-volume web-site, check out ThreadPoolExecutor and the likes.

    Whatever you decide to do, I *strongly* recommend you read the book Java
    Concurrency in Practice. It describes all the tools available to Java
    programmers and most of the caveats of multi-threaded applications in
    the Java platform.
    <http://virtualinfinity.net/wordpress/technical-book-recommendations/java-concurrency-in-practice/>


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Feb 19, 2008
    #4
    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. bhchng
    Replies:
    0
    Views:
    669
    bhchng
    Jan 13, 2004
  2. davout
    Replies:
    0
    Views:
    562
    davout
    Apr 18, 2004
  3. leon
    Replies:
    0
    Views:
    439
  4. Jeff Rodriguez
    Replies:
    23
    Views:
    1,157
    David Schwartz
    Dec 9, 2003
  5. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,483
    Arne Vajhøj
    Apr 9, 2008
Loading...

Share This Page