Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:04:21+00:00 2026-05-25T17:04:21+00:00

This builds up on my previous question . My ftp server has 10 files,

  • 0

This builds up on my previous question.

My ftp server has 10 files, say test1.txt, test2.txt and so on. I want to be able to download multiple files (max 3) at the same time. I am calling downloadFilesByPattern(....) If I dont use synchronized on downloadFile() then only some of the file are downloaded not all. If I use synchronized, then all the files are downloaded, but I don’t think they are happening in parallel. Is the issue because an instance varible is passed to all threads and a method on that instance is called by all threads.

public class FTPClientService implements IClient {

private String username;
private String password;
private String port;
private String host;
private String path;
FTPClient client = new FTPClient();

private static class DownloadTask implements Runnable {

    private String name;
    private String toPath;
    private IClient client;

    public DownloadTask(String name, String toPath, IClient client) {
        this.name = name;
        this.toPath = toPath;
        this.client = client;
    }

    @Override
    public void run() {
        System.out.println("download = " + name);
        client.downloadFile(name, toPath);
    }
}

public void downloadFilesByPattern(String fileNamePattern, final String outputFilePath) {

    if(!changeDirectory()){
        return;
    }

    try {
        //get a list of file names that match the pattern
        String[] names = client.listNames();

        ExecutorService pool = Executors.newFixedThreadPool(3);
        for (String name : names) {
            //check if the filename matches the pattern
            Pattern pattern =  Pattern.compile(fileNamePattern);
            Matcher matcher = pattern.matcher(name);
            if(matcher.find()){
                System.out.println("Match found = " + name);
                pool.submit(new DownloadTask(name, outputFilePath, this));
            }else{
                System.out.println("No match = " + name);
            }
        }
        pool.shutdown();
        try {
            pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ex) {
        }

    } catch (IOException ex) {
    }
}

public synchronized void downloadFile(String fileName, String outputFilePath) {

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFilePath+"/"+fileName);
        if(this.isFilePresent(fileName)){
            //look for fileName in the path and write it to 
            client.retrieveFile(fileName, fos);
            System.out.println("Downloading file " + fileName + " ...");
        }else{
            System.out.println("Could not find file " + fileName);
        }
    } catch (IOException ex) {
    } finally {
        try {
            fos.close();
        } catch (IOException ex) {
        }
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:04:22+00:00Added an answer on May 25, 2026 at 5:04 pm

    It’s because they all use the same instance of

    FTPClient client
    

    You need to either create new instance of FTPClientService for every download/thread or have an instance of FTPClient for every thread. I personally prefer the second variant which can be easily implemented using ThreadLocal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question that builds off of one of my previous questions .
Surprisingly I was only able to find one previous question on SO about this
This question builds off of a previously asked question: Pass by reference multidimensional array
Reading this post has left me wondering; are nightly builds ever better for a
In relation to this previous question I am trying to create a batch file
This is a follow up on my previous question: How do you map areas
Carrying on from my previous question, which has now been solved: Issue with Pointers
I know this question has been asked before, but none of the answers in
This is a continuation question from a previous question I have asked I now
OK, this kind of follows on from my previous question . What I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.