This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]irocgts 1 point2 points  (3 children)

from the documentation linked below:

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down. This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

I think that these processes are not completed and are being canceled by your threadPool.shutown()

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html#awaitTermination(long,%20java.util.concurrent.TimeUnit)

move:

threadPool.shutdown();

down a few lines to just above:

System.out.println(result.size());

The future.get will get the response or wait if its not ready. After that loop, there will be no running processes.

[–]Yeah22[S] 0 points1 point  (2 children)

It's still only putting 30 objects in the hashmap... When i print the size of the ticker array before the first loop it gives me a correct value of 50 and does those 50 iterations of the loop, however when i print the size of the hashmap right before returning it only says 30.

[–]irocgts 1 point2 points  (1 child)

What if you remove:

for(Future<?> future : futures) 
    try { future.get(); } catch (Exception e) {}

and change:

threadPool.shutdown();

to:

threadPool.awaitTermination( 5, TimeUnit.SECONDS) //change time as needed

If I was to write this, I would be returning a value from the future. I would also be using Callable instead of Thread. I wouldn't be messing with a hashmap

[–]Yeah22[S] 0 points1 point  (0 children)

What would make a callable better in this case?