Thread independent from the result

1

I have a query where returns a list of results;

Example:

192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5

I would like to start from this list creating a thread for each one independent. For each one to do its processing.

Currently I use only that waiting for one to finish for the other to process

new Thread(){
        public void run(){
            MeuController mc = new MeuController();
            List lista = mc.getMetodo();
            Iterator it = lista.iterator();
            while( it.hasNext() ){
               Model model = (Model) it.next();
               //agora vai processar
            }
         }
    }.start();

What is it like?

    
asked by anonymous 19.06.2018 / 21:31

1 answer

2

Do this:

new MeuController().getMetodo().forEach(model -> {
    new Thread(() -> {
        //agora vai processar
    }).start();
});

Or, you create a method like this:

private void processar(Model model) {
    //agora vai processar
}

And then do this:

new MeuController().getMetodo().forEach(model -> {
    new Thread(() -> processar(model)).start();
});
    
19.06.2018 / 22:37