java.util.concurrent: Blocking

T

Timo Nentwig

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" );
}
}
 
I

iksrazal

Timo Nentwig said:
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
 
T

Timo Nentwig

iksrazal said:
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!
 
I

iksrazal

Timo Nentwig said:
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
 
T

Timo Nentwig

iksrazal said:
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!
 

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

Error with server 3
Help in hangman game 1
java.util.concurrent 4
Void problem 1
Java matrix problem 3
School Project 1
Java method query 2
Implementing Many Stacks in the Same Program 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top