# Parallel activity execution java-sdk

**URL:** <https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088>\
**Category:** Community Support\
**Created:** [September 29, 2022, 6:20am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088 "2022-09-29T06:20:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Monikongkona](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/monikongkona/32/1919_2.png) [@Monikongkona](https://community.temporal.io/u/Monikongkona)\
**Post date:** [September 29, 2022, 6:20am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/1 "2022-09-29T06:20:37Z")

</div>

Hi, currently I am developing a workflow where my use case is like below.  
I have say 5M records in the database to be updated and I want to parallelise this update by say running 5 updates concurrently. However I want to create 10 parallel activities via Promise but concurrently I want to run only say 5 activities. As soon as 1 finishes 6th one from the remaining 10 parallel activity is picked up and so on.  
MaxConcurrentActivityExecutionSize is at the worker level and not workflow level right?  
Also below await way of handling it is also incorrect right?  
Workflow.await(() → activities.stream().filter(p → !p.isCompleted()).count() \< MAX\_RUNNING) - this is I believe where workflow execution will pause if #activities running crosses MAX\_RUNNING

I want to know the best way to handle this in java

---

<div class="post-metadata">

**Author:** ![krocos](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/krocos/32/328_2.png) [@krocos](https://community.temporal.io/u/krocos)\
**Post date:** [September 29, 2022, 8:37am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/2 "2022-09-29T08:37:04Z")

</div>

In my opinion you can start 5 activities with heartbeat and pass a range of records into each activity which they must to update. Then just wait all these activities finish to work and you are done.

---

<div class="post-metadata">

**Author:** ![Monikongkona](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/monikongkona/32/1919_2.png) [@Monikongkona](https://community.temporal.io/u/Monikongkona)\
**Post date:** [September 29, 2022, 9:52am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/3 "2022-09-29T09:52:58Z")

</div>

No what I need is to control the concurrency and number of activities  
So I want to say create 10 activities from the workflow but I want to limit concurrent execution to say 3.  
That way in future I may increase concurrent execution to 5 or reduce to say 2 as required.  
i.e. my workflow needs to execute 10 activities, but only want 5 activities executing concurrently. I want to start 5 activities and start more when one of the executing activities is completed.

---

<div class="post-metadata">

**Author:** ![krocos](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/krocos/32/328_2.png) [@krocos](https://community.temporal.io/u/krocos)\
**Post date:** [September 29, 2022, 10:31am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/4 "2022-09-29T10:31:45Z")

</div>

As I see activity is not well choice to create something that will waits next task.

What if to create a few child workflows each of those will wait next task or cancelation? In child workflow every time when previously signaled task ends it will signal parent that it ready to get new task to process.

Parent workflow will control the number of such child workflows and distribute tasks between them. Also the parent workflow will listen signals to size the pool of child workflows.

---

<div class="post-metadata">

**Author:** ![Monikongkona](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/monikongkona/32/1919_2.png) [@Monikongkona](https://community.temporal.io/u/Monikongkona)\
**Post date:** [September 29, 2022, 10:47am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/5 "2022-09-29T10:47:09Z")

</div>

ok so you are saying we can’t use parallel activities in this usecase. My usecase is simple I want to parallelize DB operation which as per documentation is Promise.allOf(list of activities to be run in parallel) The extra thing I am looking into is to limit those concurrent running activities in parallel.

I don’t want to Set WorkerOptions.MaxConcurrentActivityExecutionSize since that will limit number of activities executing at the same time per worker. So are you saying if you have a workflow that needs to execute 100 activities, but only want 10 activities executing concurrently. Is that not possible via activity but has to be child workflow?

---

<div class="post-metadata">

**Author:** ![antonio.perez](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/antonio.perez/32/2417_2.png) [@antonio.perez](https://community.temporal.io/u/antonio.perez)\
**Post date:** [September 29, 2022, 11:33am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/6 "2022-09-29T11:33:57Z")

</div>

Hi @Monikongkona

You can have different taskqueues according to your needs and set MaxConcurrentActivityExecutionSize only to one of them.

You could run the activity you want to limit the number of concurrent Activity Executions in one `task queue` (`taskqueueA`) and have another `task queue` for the rest of the activities. Then set up a worker with `MaxConcurrentActivityExecutionSize = x` listening on `taskqueueA`, this will limit the number of concurrent Activity Executions only for the activities running on that taskqueue.

---

<div class="post-metadata">

**Author:** ![Monikongkona](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/monikongkona/32/1919_2.png) [@Monikongkona](https://community.temporal.io/u/Monikongkona)\
**Post date:** [October 3, 2022, 4:37am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/7 "2022-10-03T04:37:38Z")

</div>

Hi @krocos /@antonio.perez

Going through promise samples in github for parallel activity execution, just have a basic confusion.  
Say as in the example, I add via async function call all the composeGreeting() methods in the activity as below  
if (names != null) {  
for (String name : names) {  
promiseList.add(Async.function(activities::composeGreeting, “Hello”, name));  
}

Now does both the below actions mean the same or are they different??  
Workflow.await(() → promiseList.stream().filter(p → !p.isCompleted()).count() \< 2);  
Promise.allOf(promiseList).get();

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [October 4, 2022, 4:59am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/9 "2022-10-04T04:59:09Z")

</div>

For this particular example technically they are the same  
(think maybe should be `Workflow.await(() → promiseList.stream().filter(p → p.isCompleted()).count() == 1);` instead but not the point).

Reason being is that Workflow.await unblock condition is going to be re-evaluated each time one of your async activities complete or fail after retries or time out.  
From best practice perspective imo its better to use use Promise.allOf cause it’s easier to define and more intuitive, as well as if you had multiple activities promises, failure of single one would be delivered to workflow code immediately (after retries), for example:

```auto
try {
  Promise.allOf(promiseList).get();
} catch (ActivityFailure e) {
  // ...
}

```

Where as with Workflow.await you would not be able to catch and handle any failures

---

<div class="post-metadata">

**Author:** ![Monikongkona](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/monikongkona/32/1919_2.png) [@Monikongkona](https://community.temporal.io/u/Monikongkona)\
**Post date:** [October 6, 2022, 6:18am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/10 "2022-10-06T06:18:13Z")

</div>

ok thanks @tihomir  
Actually the reason behind asking that question what I want to achieve is parallelism but I also want to control number of parallel activities running at any point of time.  
So I want to create 20 parallel activities however at 1 time only 5 should run concurrently . Only after any 1 of the 5 completes 6th one should start

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [October 6, 2022, 11:46am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/11 "2022-10-06T11:46:34Z")

</div>

A single worker can support a large number of parallel activity executions (with Java SDK its 200 by default per WorkerOptions config).  
You can use [WorkerOptions](https://github.com/temporalio/sdk-java/blob/master/temporal-sdk/src/main/java/io/temporal/worker/WorkerOptions.java) → [setMaxConcurrentActivityExecutionSize](https://github.com/temporalio/sdk-java/blob/master/temporal-sdk/src/main/java/io/temporal/worker/WorkerOptions.java#L124) to limit number of parallel activities.

So in your case for example:

```auto
Worker worker = factory.newWorker(TASK_QUEUE,
            WorkerOptions.newBuilder()
                    .setMaxConcurrentActivityExecutionSize(5)
                    // ...
                    .build());

```

Note that this is a per-worker configuration.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 7, 2022, 3:28am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/12 "2022-10-07T03:28:28Z")

</div>

If you want to limit the number of activities that run in parallel from the the same workflow then you have to implement the logic yourself by calling activity 5 times and starting a new activity as soon as any of them complete. You can use `Promise.anyOf` to wait for a single activity completion.

---

<div class="post-metadata">

**Author:** ![Monikongkona](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/monikongkona/32/1919_2.png) [@Monikongkona](https://community.temporal.io/u/Monikongkona)\
**Post date:** [October 7, 2022, 4:24am UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/13 "2022-10-07T04:24:02Z")

</div>

ok thanks @maxim for the reply.

Will something like this work? Basically from my WorkflowImpl class I would call the Activity in a loop.  
Here windowSize is the maxConcThreads and size is maxParallelThreads

```
    int windowSize = 5;
    int size = 20;
    int endIndex = Math.min(windowSize, size);
    AtomicInteger index = new AtomicInteger(0);

    for(; index.get()<endIndex; index.incrementAndGet()) {
        this.runTask(index, size, results);
    }

    Workflow.await(()-> index.get() == size);

```

My activity is AccountingActivity and it returns AccountingResult. runTask() will be called 1 by 1 from above.

```java
private List<AccountingResult> runTask(AtomicInteger index,
            Integer size, List<AccountingResult> results) {
        Promise<AccountingResult> promise = Async.function(accountingActivity::processTransferAccounting);

        promise.thenApply(result -> {
            results.add(result);
            if(index.get() != size){
                index.incrementAndGet();
                this.runTask(index, size, results);
            }
            return result;
        });

        return results;
    }

```

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 7, 2022, 12:03pm UTC](https://community.temporal.io/t/parallel-activity-execution-java-sdk/6088/14 "2022-10-07T12:03:09Z")

</div>

Yes, the idea looks fine.
