java.util.concurrent: Blocking

Discussion in 'Java' started by Timo Nentwig, Jun 24, 2004.

  1. Timo Nentwig

    Timo Nentwig Guest

    Hi!

    I do start 10 threads in a loop and want only 2 to be running at once.
    That works but I want pool.execute() to not return and therefore block
    the for loop if there are already 2 threads running. How am I going to
    do this?

    Timo


    import java.util.concurrent.Executors;
    import java.util.concurrent.ThreadPoolExecutor;

    class ConcurrentTest
    {
    private final ThreadPoolExecutor pool;

    public ConcurrentTest()
    {
    pool = (ThreadPoolExecutor)Executors.newFixedThreadPool( 2 );
    go();
    }

    public void go()
    {
    for( int i = 0; i < 10; i++ )
    {
    System.out.println( "start: " + i );
    pool.prestartAllCoreThreads();
    pool.execute( new Handler( new Integer( i ) ) );
    System.out.println( "\tcore:" + pool.getCorePoolSize() + " active: "
    + pool.getActiveCount() + " completed:"
    + pool.getCompletedTaskCount() );
    }
    }

    public static void main( String[] args )
    {
    new ConcurrentTest();
    }
    }

    class Handler implements Runnable
    {
    private final Integer id;

    public Handler( Integer id )
    {
    this.id = id;
    }

    public void run()
    {
    System.out.println( id );
    for( int i = 0; i < 10000; i++ )
    for( int j = 0; j < 10000; j++ );
    System.out.println( id + " done" );
    }
    }
     
    Timo Nentwig, Jun 24, 2004
    #1
    1. Advertising

  2. Timo Nentwig

    iksrazal Guest

    Timo Nentwig <> wrote in message news:<>...
    > Hi!
    >
    > I do start 10 threads in a loop and want only 2 to be running at once.
    > That works but I want pool.execute() to not return and therefore block
    > the for loop if there are already 2 threads running. How am I going to
    > do this?
    >
    > Timo
    >


    It doesn't block, you're right. The for loop completes immediately, as
    it should. Are you looking more for a producer/consumer model? The
    idea is worker threads. You have 10 short lived tasks, only 2 run at
    the same time, none of them depend on each other, and you want the 10
    to complete ASAP. PooledExecutor manages that for you. Seems like you
    need something else. Because the advantages that
    PooledExecutor/ThreadPoolExecutor offers, as I've been using it since
    before tiger and have read a bit recently on the motivations behind
    it, wouldn't have the same advantage if there was blocking(as I
    understand it).

    For example, if you were in the for loop and blocking. An exception
    was thrown in your runnable. The runnable will restart the thread, and
    not die. How would you manage that in a for loop? Manage a timeout?
    Theres the Cancellable class, but it raises a lot of little details.

    One other point: two threads are started and you want to block. Yet
    one may finish. Do you want a return then? Not until the second
    completes? Seems ineffecient.

    HTH

    Outsource to an American programmer living in brazil!
    http://www.braziloutsource.com/
    iksrazal
     
    iksrazal, Jun 24, 2004
    #2
    1. Advertising

  3. Timo Nentwig

    Timo Nentwig Guest

    iksrazal wrote:
    > It doesn't block, you're right. The for loop completes immediately, as
    > it should. Are you looking more for a producer/consumer model? The


    Doug Lea gave me the advice to acquire a Semaphores before executing a
    thread and let the thread release the Semaphore.

    Thanks, Doug!
     
    Timo Nentwig, Jun 25, 2004
    #3
  4. Timo Nentwig

    iksrazal Guest

    Timo Nentwig <> wrote in message news:<>...
    > iksrazal wrote:
    > > It doesn't block, you're right. The for loop completes immediately, as
    > > it should. Are you looking more for a producer/consumer model? The

    >
    > Doug Lea gave me the advice to acquire a Semaphores before executing a
    > thread and let the thread release the Semaphore.
    >
    > Thanks, Doug!


    Code you post the code so we can see the outcome and perhaps learn
    something along the way?

    Outsource to an American programmer living in brazil!
    http://www.braziloutsource.com/
    iksrazal
     
    iksrazal, Jun 25, 2004
    #4
  5. Timo Nentwig

    Timo Nentwig Guest

    iksrazal wrote:
    > Code you post the code so we can see the outcome and perhaps learn
    > something along the way?


    It's basically like this:

    Semaphore s = new Semaphore(CONCURRENT_THREADS, true);

    for(...)
    {
    s.acquire();
    es.execute(new Worker(s));
    }

    class Worker
    {
    Semaphore s;
    public Handler(Semaphore s)
    {
    this.s=s;
    }

    public run()
    {
    // work()
    s.release();
    }
    }

    Happy coding!

    BTW: this lib should have been part of Java *much* earlier! Excellent
    work, Doug!
     
    Timo Nentwig, Jun 25, 2004
    #5
    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. chotchkie
    Replies:
    2
    Views:
    874
    chotchkie
    Aug 4, 2005
  2. Pep
    Replies:
    6
    Views:
    840
  3. Let me Think

    java.util.concurrent

    Let me Think, Oct 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,908
  4. Ake L
    Replies:
    7
    Views:
    4,289
    Ake L
    Dec 11, 2005
  5. Replies:
    3
    Views:
    6,659
    Roedy Green
    Feb 24, 2006
Loading...

Share This Page