Skip to main content
removeu 21 caracteres do conteúdo; tags editadas
Fonte Link
Anthony Accioly
  • 21,2mil
  • 2
  • 54
  • 109
for( Item item : result ) {
                Callable< List< IResult > > callable = new HTMLParser( item ); 
                Future< List< IResult > > future = pool.submit( callable );
                set.add( future );
            }
    
            for( Future< List< IResult > >  future : set ) {
                List< IResult > result = future.get( ); // wait for a processor to complete
                if( result != null && !result.isEmpty( ) )
                    FResults.addAll( result ); 
            }
}
for( Item item : result ) {
                Callable< List< IResult > > callable = new HTMLParser( item ); 
                Future< List< IResult > > future = pool.submit( callable );
                set.add( future );
            }
    
            for( Future< List< IResult > >  future : set ) {
                List< IResult > result = future.get( ); // wait for a processor to complete
                if( result != null && !result.isEmpty( ) )
                    FResults.addAll( result ); 
            }
for( Item item : result ) {
    Callable< List< IResult > > callable = new HTMLParser( item ); 
    Future< List< IResult > > future = pool.submit( callable );
    set.add( future );
}
    
for( Future< List< IResult > >  future : set ) {
    List< IResult > result = future.get( ); // wait for a processor to complete
    if( result != null && !result.isEmpty( ) )
        FResults.addAll( result ); 
    }
}
Fonte Link
user2989745
  • 399
  • 1
  • 4
  • 15

Usando o Callable em java executa sequencialmente o código

Estou a fazer um servidor em java queria executar determinadas tarefas em paralelo. Tenho uma lista de um determinado tipo quero que 20 threads trate de deterimados elementos da lista.

for( Item item : result ) {
                Callable< List< IResult > > callable = new HTMLParser( item ); 
                Future< List< IResult > > future = pool.submit( callable );
                set.add( future );
            }
    
            for( Future< List< IResult > >  future : set ) {
                List< IResult > result = future.get( ); // wait for a processor to complete
                if( result != null && !result.isEmpty( ) )
                    FResults.addAll( result ); 
            }

Só que executando esta soluçao, analisando os logs parece me que está a correr as threads de forma sequencial

2016-12-06 14:17:02.106 DEBUG 18229 --- [pool-1-thread-5] ......
2016-12-06 14:17:02.106 DEBUG 18229 --- [pool-1-thread-5] ....
2016-12-06 14:17:02.106 DEBUG 18229 --- [pool-1-thread-5] .....
2016-12-06 14:17:02.106 DEBUG 18229 --- [pool-1-thread-5] .......
2016-12-06 14:17:02.106  INFO 18229 --- [pool-1-thread-5] .....
2016-12-06 14:17:02.243 DEBUG 18229 --- [ool-1-thread-19] .....
2016-12-06 14:17:02.243  INFO 18229 --- [ool-1-thread-19] ....
2016-12-06 14:17:02.587 DEBUG 18229 --- [ool-1-thread-16] .....
2016-12-06 14:17:02.587 DEBUG 18229 --- [ool-1-thread-16] ....
2016-12-06 14:17:02.587 DEBUG 18229 --- [ool-1-thread-16] .....
2016-12-06 14:17:02.587 DEBUG 18229 --- [ool-1-thread-16] .....
2016-12-06 14:17:02.588 DEBUG 18229 --- [ool-1-thread-16] .....
2016-12-06 14:17:02.588 DEBUG 18229 --- [ool-1-thread-16] ......
2016-12-06 14:17:02.588 DEBUG 18229 --- [ool-1-thread-16] .....
2016-12-06 14:17:02.588 DEBUG 18229 --- [ool-1-thread-16] ....
2016-12-06 14:17:02.588 DEBUG 18229 --- [ool-1-thread-16] ......
2016-12-06 14:17:02.588 DEBUG 18229 --- [ool-1-thread-16] ...

Os logs de uam thread aparece sequencialmente todo seguido, não me faz sentido. Alguma ideia ?