0

ワーカーが解放されるとすぐに実行されるように、スレッド プール内のタスクをキューに入れようとしています。これに関するさまざまな例を見つけましたが、すべての例で、ジョブごとに新しいワーカー インスタンスを使用するように設定されています。永続的な労働者が欲しい。

ftp バックアップ ツールを作成しようとしていますが、機能していますが、単一の接続の制限のために低速です。私が理想的にやりたいことは、ディレクトリをスキャンしてファイルリストを作成するための単一の接続を持ち、その後4人のワーカーがそのファイルをダウンロードすることです。

以下は、私の FTP ワーカーの例です。

public class Worker implements Runnable {
  protected FTPClient _ftp;

  // Connection details
  protected String _host = "";
  protected String _user = "";
  protected String _pass = "";

  // worker status
  protected boolean _working = false;

  public Worker(String host, String user, String pass) {
    this._host = host;
    this._user = user;
    this._pass = pass;
  }

   // Check if the worker is in use
  public boolean inUse() {
    return this._working;
  }

  @Override
  public void run() {
    this._ftp = new FTPClient();
    this._connect();
  }

  // Download a file from the ftp server
  public boolean download(String base, String path, String file) {
    this._working   = true;
    boolean outcome = true;

    //create directory if not exists
    File pathDir = new File(base + path);
    if (!pathDir.exists()) {
      pathDir.mkdirs();
    }

    //download file
    try {
      OutputStream output = new FileOutputStream(base + path + file);
      this._ftp.retrieveFile(file, output);
      output.close();
    } catch (Exception e) {
      outcome = false;
    } finally {
      this._working = false;
      return outcome;
    }
  }

  // Connect to the server
  protected boolean _connect() {
    try {
      this._ftp.connect(this._host);
      this._ftp.login(this._user, this._pass);
    } catch (Exception e) {
      return false;
    }
    return this._ftp.isConnected();
  }

  // Disconnect from the server
  protected void _disconnect() {
    try {
      this._ftp.disconnect();
    } catch (Exception e) { /* do nothing */ }
  }
}

Worker.download(...)ダウンロードごとに ftp サーバーへの新しい接続を作成することなく、ワーカーが利用可能になるたびに、キュー内の各タスクを呼び出すことができるようにしたいと考えています。

私はこれまでスレッドを使用したことがなく、現在はぐるぐる回っているので、どんな助けもいただければ幸いです。

4

2 に答える 2