How to upload multiple files at the same time using the dropbox java api

寵の児 提交于 2019-12-06 10:21:14

Yes, you can call the API using multiple threads and upload files. You can use Thread Pools for the same. You need to identify the point for creating the number of threads that will not impact performance.

Below code will let you upload 10 files(provided in fileLocations array) in 5 separate threads.

public class UploadThread implements Runnable {

    private String fileLocation;

    public UploadThread(String s){
        this.fileLocation=s;
    }

    @Override
    public void run() {
       //your api call to upload file using fileLocation
    }

    @Override
    public String toString(){
        return this.command;
    }
}

public class UploadExecutor{

    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(5);

        String[] fileLocations = new String[10];

        for (int i = 0; i < 10; i++) {

            Runnable worker = new UploadThread(fileLocations[i]);

            executor.execute(worker);
        }
        executor.shutdown();

        while (!executor.isTerminated()) { }

        System.out.println("Finished uploading");
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!