简体   繁体   English

使用ScheduledExecutorService同时运行多个线程

[英]Running multiple threads concurrenty using ScheduledExecutorService

The below code executes a file transfer task periodically. 以下代码定期执行文件传输任务。 I want to do another file copy task at same time from different location by calling the same method.For that i created another runnable2 and executor2 instances and executed. 我想通过调用同一方法从不同位置同时执行另一个文件复制任务。为此,我创建了另一个runnable2和executor2实例并执行。 How to do multiple tasks using a single instance instead of creating multiple instances. 如何使用单个实例而不是创建多个实例来执行多个任务。

public static void main(String[] args) {

        Runnable runnable = new Runnable() {
          public void run() {

            File srcFolder = new File("c:\\location1\\Test1\\");
            File destFolder = new File("d:\\location1\\Test1\\");

            if(!srcFolder.exists()){
               System.out.println("Directory does not exist.");
               System.exit(0);
            }else{
               try{
                copyFolder(srcFolder,destFolder);
               }catch(IOException e){
                e.printStackTrace();
                System.exit(0);
               }
            }
            System.out.println("Done");           
            }
        };

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);    
    executor.scheduleAtFixedRate(runnable, 0, 300, TimeUnit.SECONDS);
}

if you are going to do the same operation more than one time, then creating a class is better than anonymous class (make it have 2 params in constructor): 如果您打算多次执行相同的操作,那么创建一个类要好于匿名类(使它在构造函数中具有2个参数):

public class FileCopy implements Runnable {

    private String src="", dest = "";
    public FileCopy(String src, String dest){
        this.src  = src;
        this.dest = dest;
    }

    @override
    public void run(){
        File srcFolder = new File(src);
        File destFolder = new File(dest);

        if(!srcFolder.exists()){
           System.out.println("Directory does not exist.");
           //System.exit(0);
        }else{
           try{
            copyFolder(srcFolder,destFolder);
           }catch(IOException e){
            e.printStackTrace();
            //System.exit(0);
           }
        }
        System.out.println("Done"); 
    }//run
}

now you can make as many instances as you want, pass different params each time, and execute the Runnable 现在您可以根据需要创建任意多个实例,每次都传递不同的参数,并执行Runnable

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);    
executor.scheduleAtFixedRate(new FileCopy(src1, dest1), 0, 300, TimeUnit.SECONDS);
executor.scheduleAtFixedRate(new FileCopy(src2, dest2), 0, 300, TimeUnit.SECONDS);

Notes: : 注意事项

  • may be you need to change this newScheduledThreadPool(1) to newScheduledThreadPool(2) ? 可能您需要将此newScheduledThreadPool(1)更改为newScheduledThreadPool(2)吗? not so sure 不太确定

  • System.exit(0); is not an option now, in case of errors, because you have multiple instances running, if one has a problem, that should not mean closing the whole app, you need to implement another way to handle errors 在出现错误的情况下,现在不是一种选择,因为您正在运行多个实例,如果一个实例有问题,这并不意味着关闭整个应用程序,则需要实现另一种方式来处理错误

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

相关问题 使用ScheduledExecutorService和Service中的newSingleThreadScheduledExecutor安排任务,导致任务在多个线程中运行 - Scheduling a task using ScheduledExecutorService with newSingleThreadScheduledExecutor in Service resulting in task running in multiple threads ScheduledExecutorService并行多个线程 - ScheduledExecutorService multiple threads in parallel 使用ScheduledExecutorService以固定的间隔并行运行多个线程 - Multiple threads to run parallel at fixed interval using ScheduledExecutorService ScheduledExecutorService在使用Executors.newSingleThreadScheduledExecutor创建它时运行多个线程 - ScheduledExecutorService running many threads when I created it using Executors.newSingleThreadScheduledExecutor Java 中的 ScheduledExecutorService 和线程 - ScheduledExecutorService and Threads in Java 由ScheduledExecutorService触发的同步线程 - Synchronize Threads triggered by ScheduledExecutorService ScheduledExecutorService 不创建并发线程 - ScheduledExecutorService not creating concurrent threads 使用ScheduledExecutorService在线程上执行Runnable lambda - Execute Runnable lambda with more than on threads using ScheduledExecutorService 运行多个线程 - Running Multiple Threads 在Asynctask中运行多个线程 - Running multiple threads in Asynctask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM