CountDownLatch (a synchronization helper class)

CountDownLatch, a synchronization helper class that allows one or more threads to wait until a set of operations that are being performed in other threads are completed.

main method

 public CountDownLatch(int count);

 public void countDown();

 public void await() throws InterruptedException
 

The constructor parameter specifies the number of times to count

countDown method, the current thread calls this method, the count is decremented by one

await method, calling this method will block the current thread until the value of the timer is 0, ensuring thread synchronization.

 

E.g:

 Baiye snippet code:

public Appclient buildApp(final AppParam appDTO) throws Exception {
        final Appclient client = new Appclient();
        client.setAppid(appDTO.getAppid());
        int count = appDTO.getPlatformAndriod() + appDTO.getPlatformIos();
        final CountDownLatch countdown = new CountDownLatch(count);
        if (appDTO.getPlatformAndriod() == 1) {
            Thread androidThread = new Thread() {
                public void run() {
                    try {
                        String releaseUrl = buildAndriod(appDTO, client);
                        if (releaseUrl != null)
                            client.setAndroidFile(releaseUrl);
                        countdown.countDown();
                        logger.info("Build android finished:" + releaseUrl);
                    } catch (Exception e) {
                        countdown.countDown();
                        logger.error(e.getMessage(), e);
                    }
                }
            };
            androidThread.start();
        }
        if (appDTO.getPlatformIos() == 1) {
            Thread iosThread = new Thread() {
                public void run() {
                    try {
                        String releaseUrl = null;
                        releaseUrl = buildIos(appDTO, client);
                        if (releaseUrl != null)
                            client.setIosFile(releaseUrl);
                        countdown.countDown();
                        logger.info("Build ios finished:" + releaseUrl);
                    } catch (Exception e) {
                        countdown.countDown();
                        logger.error(e.getMessage(), e);
                    }

                }
            };
            iosThread.start();
        }
        countdown.await();
        return client;
    }

 

 

public String buildAndriod(AppParam appDTO, Appclient client)
            throws Exception {
        // 1.Create build directory
        String appBuildDir = Utility.APP_PKG_DIR + "/app" + appDTO.getAppid()
                + "/android";
        FileUtil.createDir(appBuildDir);
        String andriodAppFileName = Cn2Spell.converterToSpell(appDTO.getName())
                .replaceAll(" ", "_").replaceAll("_", "");
        String andriodAppFile = "/app" + appDTO.getAppid() + "/android/"
                + andriodAppFileName + "-release.apk";
        String downloadUrlStr =andriodAppFile;
        // 2.Create config.xml
        Document document = DocumentHelper.createDocument(); // create document
        Element root = document.addElement("root");
        Element appname = root.addElement("appname");
        Element appid = root.addElement("appid");
        Element apkname = root.addElement("appfilename");
        Element packagename = root.addElement("packagename");
        Element desc = root.addElement("description");
        Element downloadUrl = root.addElement("downloadurl");
        Element loadURL = root.addElement("loadURL");
        Element p12password = root.addElement("p12password");// ios预留字段

        p12password.setText("test");
        String package_name = "by"
                + Cn2Spell.converterToSpell(appDTO.getName())
                        .replaceAll(" ", "").replaceAll("_", "")
                + Random.getRandomInt(4);
        loadURL.setText(Config.getString("appH5Url")+appDTO.getUrl());
        appname.setText (appDTO.getName());// set app name
        appid.setText(appDTO.getAppid());// h5 address
        apkname.setText(andriodAppFileName);// set app name
        packagename.setText(package_name);// set Package name
        desc.setText("");//
        downloadUrl.setText(downloadUrlStr);
        // Set Android related configuration
        client.setApkName(Cn2Spell.converterToSpell(appDTO.getName()));
        client.setPackageName(package_name);

        XMLWriter xmlWriter = null;
        // generate configuration file
        String configFile = appBuildDir + "/build_config.xml";
        try {
            OutputFormat outFormat = OutputFormat.createPrettyPrint();
            outFormat.setEncoding("UTF-8");
            outFormat.setTrimText(false);
            xmlWriter = new XMLWriter(new FileOutputStream(configFile),
                    outFormat);
            xmlWriter.write(document);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        // 4.Copy img file from net
        String apkImgDir = appBuildDir + "/png/";
        FileUtil.createDir(apkImgDir);
        String srcFile = Constant.PIC_SERVER_VPN + appDTO.getIcon();
        String dstfile = apkImgDir + Utility.APP_ANDRIOD_ICON_NAME;
        ImgHelper.writeImg(srcFile, dstfile);

        // 4.Run build cmd
        JavaShellUtil shellUtil = new JavaShellUtil();

        dstfile = appBuildDir + "/build_android.sh";
        FileUtil.copyFile(new File(Utility.SH_DIR), new File(dstfile));

        String shellCmd = "sh " + appBuildDir + "/build_android.sh "
                + appBuildDir;
        logger.info("sh command:" + shellCmd);
        shellUtil.executeShell(shellCmd);

        FastdfsUtil fast = new FastdfsUtil();
        String[] rsPath = fast.uploadFile(appBuildDir + "/"
                + andriodAppFileName + "-release.apk","apk", null);
        downloadUrlStr = "/" + rsPath[0] + "/" + rsPath[1];

        // 5.Check build status
        logger.info("Android DownloadFile:" + Utility.APP_PKG_DIR
                + andriodAppFile);
        if (FileUtil.isFileExist(Utility.APP_PKG_DIR + andriodAppFile)) {
            // appDTO.setAndriodAppFile(andriodAppFile);
            return downloadUrlStr;
        } else {
            return null;
        }
    }

    /**
     * 生成IOS客户端
     *
     * @param appDTO
     * @param client
     * @return
     * @throws Exception
     */
    public String buildIos(AppParam appDTO, Appclient client) throws Exception {
        // 1.Create build directory
        String appBuildDir = Utility.APP_PKG_DIR + "/app" + appDTO.getAppid()
                + "/ios";
        FileUtil.createDir(appBuildDir);
        String iosAppFileName = Cn2Spell.converterToSpell(appDTO.getName())
                .replaceAll(" ", "_").replaceAll("_", "");
        String iosAppFile = "/app" + appDTO.getAppid() + "/ios/"
                + iosAppFileName + "-release.ipa";
        String downloadUrlStr =iosAppFile;
        // 2.Create config.xml
        Document document = DocumentHelper.createDocument(); // 创建文档
        Element root = document.addElement("root");
        Element appname = root.addElement("appname");
        Element appid = root.addElement("appid");
        Element apkname = root.addElement("appfilename");
        Element packagename = root.addElement("packagename");
        Element desc = root.addElement("description");
        Element downloadUrl = root.addElement("downloadurl");
        Element loadURL = root.addElement("loadURL");
        Element p12password = root.addElement("p12password");// ios预留字段

        p12password.setText("123456");
        String package_name = "by"
                + Cn2Spell.converterToSpell(appDTO.getName())
                        .replaceAll(" ", "").replaceAll("_", "")
                + Random.getRandomInt(4);
        loadURL.setText(Config.getString("appH5Url")+appDTO.getUrl());
        appname.setText(appDTO.getName());// Set app name
        appid.setText(appDTO.getAppid());// h5 Address
        apkname.setText(iosAppFileName);// Set application name
        packagename.setText(package_name);// Set package name
        desc.setText("");//
        downloadUrl.setText(downloadUrlStr);
        // Set IOS related configuration
        client. setApkName(Cn2Spell.converterToSpell(appDTO.getName()));
        client.setPackageName(package_name);

        XMLWriter xmlWriter = null;
        // Generate configuration file
        String configFile = appBuildDir + "/build_config.xml";
        try {
            OutputFormat outFormat = OutputFormat.createPrettyPrint();
            outFormat.setEncoding("UTF-8");
            outFormat.setTrimText(false);
            xmlWriter = new XMLWriter(new FileOutputStream(configFile),
                    outFormat);
            xmlWriter.write(document);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        // 4.Copy img file from net
        String apkImgDir = appBuildDir + "/png/";
        FileUtil.createDir(apkImgDir);
        String srcFile = Constant.PIC_SERVER_VPN + appDTO.getIcon();
        String dstfile = apkImgDir + Utility.APP_IOS_ICON_NAME1;
        String dstfile2 = apkImgDir + Utility.APP_IOS_ICON_NAME2;
        ImgHelper.writeImg(srcFile, dstfile);
        ImgHelper.writeImg(srcFile, dstfile2);
        // 4.Run build cmd
        JavaShellUtil shellUtil = new JavaShellUtil();

        dstfile = appBuildDir + "/build_ios.sh";
        FileUtil.copyFile(new File(Utility.SH_DIR_IOS), new File(dstfile));

        String shellCmd = "sh " + appBuildDir + "/build_ios.sh " + appBuildDir;
        logger.info("sh command:" + shellCmd);
        shellUtil.executeShell(shellCmd);

        FastdfsUtil fast = new FastdfsUtil();
        String[] rsPath = fast.uploadFile(appBuildDir + "/" + iosAppFileName
                + "-release.ipa", "ipa", null);
        String ipa= Config.getString("APP_DOWNLOAD_SERVER_IOS")
                + rsPath[0] + "/" + rsPath[1];
        //生成plist文件
        String[] rsPathIcon = fast.uploadFile( apkImgDir + Utility.APP_IOS_ICON_NAME1, "png", null);
        String iconUrl= Config.getString("APP_DOWNLOAD_SERVER_IOS")
                + rsPathIcon[0] + "/" + rsPathIcon[1];
        String iconUrl1=iconUrl+"?filename="+Utility.APP_IOS_ICON_NAME1;
        String iconUrl2=iconUrl+"?filename="+Utility.APP_IOS_ICON_NAME2;
        String plistContent=IosPlist.getPlist(ipa, iconUrl1,iconUrl2, package_name, iosAppFileName);
       
        String []plist=fast.uploadFile(plistContent.getBytes(), "plist", null);
        downloadUrlStr="/"
                + plist[0] + "/" + plist[1];
        // 5.Check build status
        logger.info("ios DownloadFile:" + Utility.APP_PKG_DIR + iosAppFile);
        if (FileUtil.isFileExist(Utility.APP_PKG_DIR + iosAppFile)) {
            return downloadUrlStr;
        } else {
            return null;
        }
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326655689&siteId=291194637