user506710
user506710

Reputation:

Executor in java

I was trying to run ExecutorService object with FixedThreadPool and I ran into problems.

I expected the program to run in nanoseconds but it was hung. I found that I need to use Semaphore along with it so that the items in the queue do not get added up.

Is there any way I can come to know that all the threads of the pool are used.

Basic code ...

static ExecutorService pool = Executors.newFixedThreadPool(4);
static Semaphore permits = new Semaphore(4);
try {
    permits.acquire();
    pool.execute(p);  // Assuming p is runnable on large number of objects
    permits.release();
} catch ( InterruptedException ex ) {
}

This code gets hanged and I really don't know why. How to know if pool is currently waiting for all the threads to finish?

Upvotes: 0

Views: 1643

Answers (3)

SimonJ
SimonJ

Reputation: 21306

By default, if you submit more than 4 tasks to your pool then the extra tasks will be queued until a thread becomes available.

The blog you referenced in your comment uses the semaphore to limit the amount of work that can be queued at once, which won't be a problem for you until you have many thousands of tasks queued up and they start eating into the available memory. There's an easier way to do this, anyway - construct a ThreadPoolExecutor with a bounded queue.* But this isn't your problem.

If you want to know when a task completes, notice that ExecutorService.submit() returns a Future object which can be used to wait for the task's completion:

Future<?> f = pool.execute(p);
f.get();
System.out.println("task complete");

If you have several tasks and want to wait for all of them to complete, either store each Future in a list and then call get() on each in turn, or investigate ExecutorService.invokeAll() (which essentially does the same but in a single method call).

You can also tell whether a task has completed or not:

Future<?> f = pool.execute(p);

while(!f.isDone()) {
    // do something else, task not complete
}

f.get();

Finally, note that even if your tasks are complete, your program may not exit (and thus appears to "hang") if you haven't called shutdown() on the thread pool; the reason is that the threads are still running, waiting to be given more work to do.

*Edit: sorry, I just re-read my answer and realised this part is incorrect - ThreadPoolExecutor offers tasks to the queue and rejects them if they aren't accepted, so a bounded queue has different semantics to the semaphore approach.

Upvotes: 3

Kevin D
Kevin D

Reputation: 3594

You could change to using a ThreadPoolExecutor. It contains a getActiveCount() method which returns an approximate count of the active threads. Why it is approximate I'm not sure.

Upvotes: 0

gfelisberto
gfelisberto

Reputation: 1723

You do not need the Semaphore.

If you are hanging it is probably because the threads are locking themselves elsewhere.

Run the code in a Debuger and when it hangs pause it and see what the threads are doing.

Upvotes: 1

Related Questions