天天看點

多線程程式設計模式-Promise

是一種異步程式設計模式。可以先開始一個任務的執行,并獲得一個任務執行的憑證。等需要這個任務執行的結果時,再憑借方法擷取任務執行結果。 

使用Future程式設計的例子如下:

package promise;

import java.io.File;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

public class DataSyncTask implements Runnable {
    private final Map<String, String> taskParameters;

    public DataSyncTask(Map<String, String> taskParameters) {
        this.taskParameters = taskParameters;
    }

    @Override
    public void run() {
        String ftpServer = taskParameters.get("server");
        String ftpUserName = taskParameters.get("userName");
        String password = taskParameters.get("password");

        // 初始化ftp用戶端執行個體
        Future<FtpClientUtil> ftpClientUtilPromise =
                FtpClientUtil.newInstance(ftpServer, ftpUserName, password);

        // 查詢資料庫生成本地檔案
        generateFilesFromDB();

        FtpClientUtil ftpClientUtil = null;
        try {
            // 擷取初始化完畢的FTP用戶端執行個體
            ftpClientUtil = ftpClientUtilPromise.get();
        } catch (InterruptedException e) {
            ;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        uploadFiles(ftpClientUtil);
    }

    private void generateFilesFromDB() {

    }

    private void uploadFiles(FtpClientUtil ftpClientUtil) {
        Set<File> files = retrieveGeneratedFiles();

        for (File file : files) {
            try {
                ftpClientUtil.upload(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private Set<File> retrieveGeneratedFiles() {
        Set<File> files = new HashSet<>();
        return files;
    }
}
           
package promise;

import sun.net.ftp.FtpProtocolException;
import sun.net.ftp.FtpReplyCode;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 模式角色:Promise.Promisor
 *         Promise.Result
 */
public class FtpClientUtil {
    private final FtpClient ftp = new FtpClient();

    private final Map<String, Boolean> dirCreateMap = new HashMap<>();

    private FtpClientUtil() {

    }

    private volatile static ThreadPoolExecutor threadPoolExecutor;

    static {
        threadPoolExecutor = new ThreadPoolExecutor(1, Runtime.getRuntime().availableProcessors() * 2,
                60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
            }
        }, new ThreadPoolExecutor.CallerRunsPolicy());
    }

    /**
     * 模式角色:Promise.Promisor.compute
      */
    public static Future<FtpClientUtil> newInstance(final String ftpServer, final String userName,
                                                    final String password) {
        Callable<FtpClientUtil> callable = new Callable<FtpClientUtil>() {
            @Override
            public FtpClientUtil call() throws Exception {
                FtpClientUtil self = new FtpClientUtil();
                self.init(ftpServer, userName, password);
                return self;
            }
        };

        // task相當于模式角色Promise.Promise
        final FutureTask<FtpClientUtil> task = new FutureTask<>(callable);

        /**
         * 下面建立的線程相當于模式角色:Promise.TaskExecutor
         */
        new Thread(task).start();
        return task;
    }

    private void init(final String ftpServer, final String userName,
                      final String password) throws IOException, FtpProtocolException {
        FtpClientConfig config = new FtpClientConfig();
        ftp.configure(config);

        ftp.connect(ftpServer);

        System.out.println(ftp.getLastResponseString());

        FtpReplyCode reply = ftp.getLastReplyCode();
        if (!reply.isPositiveCompletion()) {
            ftp.close();
            throw new RuntimeException("FTP server refused connection.");
        }

        boolean isOK = ftp.login(userName, password);
        if (isOK) {
            System.out.println(ftp.getLastResponseString());
        } else {
            throw new RuntimeException("Failed to login." + ftp.getLastResponseString());
        }

        reply = ftp.cwd("~/subspsync");
        if (!reply.isPositiveCompletion()) {
            ftp.close();
            throw new RuntimeException("Failed to change working directory.reply:" + reply);
        } else {
            System.out.println(ftp.getLastResponseString());
        }
        ftp.setFileType(FTP.ASCII_FILE_TYPE);
    }

    public void upload(File file) throws Exception {
        InputStream dataIn = new BufferedInputStream(new FileInputStream(file), 1024 * 8);
        boolean isOK;
        String dirName = file.getParentFile().getName();
        String fileName = dirName + "/" + file.getName();
        ByteArrayInputStream checkFileInputStream = new ByteArrayInputStream("".getBytes());

        try {
            if (!dirCreateMap.containsKey(dirName)) {
                ftp.makeDirectory(dirName);
                dirCreateMap.put(dirName, null);
            }

            try {
                isOK = ftp.storeFile(fileName, dataIn);
            } catch(IOException e) {
                throw new RuntimeException("Failed to upload." + file, e);
            }

            if (isOK) {
                ftp.storeFile(fileName + ".c", checkFileInputStream);
            } else {
                throw new RuntimeException("Failed to upload " + file + ", reply:" +
                "," + ftp.getLastResponseString());
            }
        } finally {
            dataIn.close();
        }
    }

    public void disconnect() {
        if (ftp.isConnected()) {
            try {
                ftp.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
}