JSON array and multi threading

2k views Asked by At

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

1

There are 1 answers

1
abstractnature On

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