简体   繁体   English

运行多个线程

[英]Running Multiple Threads

I am using 1 thread for 2 tasks 我正在使用1个线程执行2个任务

  1. Reading file 读取文件
  2. Download file 下载文件

     scheduledExecutorService1 = Executors.newScheduledThreadPool(1); 

But with multiple files, there will be a delay per thread. 但是对于多个文件,每个线程都会有一个延迟。

Using the following code causes duplicates : 使用以下代码会导致重复

scheduledExecutorService1 = Executors.newScheduledThreadPool(2);

What will be the correct code to run with multiple threads handling delays and avoiding dulication. 与多个线程一起处理延迟并避免重复的代码将是正确的代码。

It is better to create two ExecutorService one for reading file another for downloading file. 最好创建两个ExecutorService,一个用于读取文件,另一个用于下载文件。

       ExecutorService readService = Executors.newScheduledThreadPool(1);

       ExecutorService downloadService = Executors.newScheduledThreadPool(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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