JSON array and multi threading

558
July 04, 2017, at 03:47 AM

I have a JSON array containing around 10,000 JSON objects. I want to iterate the array and call a java method to process each JSON object. I want to have few threads, may be 4 to process them simultaneously.

I want to know if this is the correct way of doing it or is there any better solution

Answer 1

Good question.

JsonArray consists of multiple JsonObjects.

A way to simultaneously process the jsonObjects in the array, using java 8 is using IntStream, with parallStream(), like this.

int size = jsonArray.size();
// Suppose you want 5 threads to do the tasks
ForkJoinPool forkJoinPool = new ForkJoinPool(5);
forkJoinPool.submit(() ->
IntStream.range(1, size).parallel().filter( i -> { YourFunction(jsonArray.get(i))};);

Edit:

Make sure your function is threadSafe

Rent Charter Buses Company
READ ALSO
JDBI Select In using fluent interface (not object queries)

JDBI Select In using fluent interface (not object queries)

Others have asked how to do this with object queries

407
Spring Amqp : Purpose of getRequiredQueueNames method

Spring Amqp : Purpose of getRequiredQueueNames method

What is the real purpose of the protected String[] AbstractMessageListenerContainer #getRequiredQueueNames() method

307