简体   繁体   中英

JAVA Executing stack of threads by n

maybe someone could recommend some good examples concerning thread executions, thread management. Maybe not only examples but article, tutorial if you will with examples.

Generally I have a problem where I need to download a bunch of files from the web, but connection is limited to two. So when I gather up all the url's to files I need, I'd like to download say... 100 files but do so in async manner by two until all the threads finish their job.

Thank, you for support.

An ExecutorService should take care of thread management for you.

final int POOL_SIZE = 2;
final ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE);

you can then call the submit method to execute runnable tasks

使用使用大小为2的固定线程池ExecutorService ,然后所有任务提交到该ExecutorService

我认为ThreadPoolExecutor应该可以为您提供帮助,在java.util.concurrent。*中有很多此类轮询的实现。

I don't have an article, but I do know of a good book that covers general multi-threaded programming using Java. It is called Java Concurrency in Practice. It does cover general usage patterns, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM