Is this considers "best practice" for a for loop implementation ?

1,047 views
Skip to first unread message

Noam Guy

unread,
Jun 21, 2016, 8:56:26 AM6/21/16
to vert.x
Hi Guys,

Is this considers "best practice" for loop implementation ?

    public void aFunction(List<String> theArrayList,Handler<Boolean> handler) {


       
AtomicInteger   total       =   new AtomicInteger(0);
       
int             arraySize   =   theArrayList.size();


       
for(String stringEntity : theArrayList){
           
            someAsyncFunc
(stringEntity, result -> {
               
if (result.succeeded()) {
                   
if(total.incrementAndGet() == arraySize)
                        handler
.handle(true);
               
} else {
                    handler
.handle(false);
               
}
           
});
           
       
}
   
}


Thanks :-)

Alexander Lehmann

unread,
Jun 21, 2016, 2:17:30 PM6/21/16
to vert.x
Do you want to start all operations in parallel or would you like to start one operation after another?

Noam Guy

unread,
Jun 21, 2016, 3:00:06 PM6/21/16
to vert.x
As far as I understand this will go parallel..
And for a sequential loop I would use a Future with compose.

Any other/better option?

Grant Haywood

unread,
Jun 21, 2016, 3:22:29 PM6/21/16
to ve...@googlegroups.com
I believe this would be concurrent but not parallel, because it all run inside of a single vertical.

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/679205c7-8649-4c0d-9ebd-7d3fc92e1694%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Noam Guy

unread,
Jun 22, 2016, 3:16:00 AM6/22/16
to vert.x
Doesn't it depends on how deep the async callbacks go?

If there is only one function in the loop - then no parallelism (in the loop)
If there is a chain of functions in the loop - then there will be context switching and therefor parallelism.

Am I missing something?

Grant Haywood

unread,
Jun 22, 2016, 2:25:03 PM6/22/16
to ve...@googlegroups.com
well, i think, based on reading this
http://vertx.io/docs/vertx-core/java/#_verticle_types

that on a Standard Vertical, you code will only be executing on 1 thread at any given time.



--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.

Noam Guy

unread,
Jun 23, 2016, 3:05:45 AM6/23/16
to vert.x
I get it - you are right :-)

So the loop above will run one after the other - correct?

And if I want a parallel loop? Should I use Future?

Thanks Grant :-)



Julien Viet

unread,
Jun 23, 2016, 3:12:16 AM6/23/16
to ve...@googlegroups.com
hi,

what do you want to make parallel (i.e on 2 cores of the same machine, executing truly at the same time) and for what purpose ?

A single event loop is not blocking and does not prevent your futures to execute concurrently, specially if they are doing IO related work.

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.

Noam Guy

unread,
Jun 23, 2016, 3:56:07 AM6/23/16
to vert.x
For example - I would like to insert into the database 100 records (that I already have in an in memory array) in a parallel way.

One way is to send it to a worker.
The other would be using Future array?

Alexander Lehmann

unread,
Jun 23, 2016, 7:18:21 AM6/23/16
to vert.x
The order of execution is different when you call other operations that are executed by vertx when they run asynchronously, for example if you do a http client request or a db insert, the execution in still in a single thread, but when the execution of the code is waiting for the http response for example, it will not occupy the thread.

Since you are calling everything async, you have a handler for each operation you start and you can collect the finished handlers in the way you have written in your first example.

It is also possible to use Futures and a combined Future to wait for all of them, if this is easier to understand, but the execution of the operations is basically the same.

When you do sql operations, it might be better to create a batch and sent that to the database in one operation, if you are calling a few http services, it might be good to start all at once.

Noam Guy

unread,
Jun 23, 2016, 8:49:46 AM6/23/16
to vert.x
Ok - thank's got it :-)

BTW - i couldn't find a way to use vertx jdbc driver to do batch INSERTS (unless you means separating the commands with a semi column)..

Thomas Segismont

unread,
Jun 23, 2016, 11:12:45 AM6/23/16
to ve...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages