小伙伴们,我开了一家海淘护肤品淘宝店,搜索店铺“禾子蝶的海淘铺”,正品保证,欢迎进店选购哦。谢谢!
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
主要方法
public CountDownLatch(int count);
public void countDown();
public void await() throws InterruptedException
构造方法参数指定了计数的次数
countDown方法,当前线程调用此方法,则计数减一
awaint方法,调用此方法会一直阻塞当前线程,直到计时器的值为0,保证线程同步。
例如:
Baiye片段代码:
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(); // 创建文档
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());// 设置app 名字
appid.setText(appDTO.getAppid());// h5地址
apkname.setText(andriodAppFileName);// 设置应用名
packagename.setText(package_name);// 设置包名
desc.setText("");//
downloadUrl.setText(downloadUrlStr);
// 设置Android相关配置
client.setApkName(Cn2Spell.converterToSpell(appDTO.getName()));
client.setPackageName(package_name);
XMLWriter xmlWriter = null;
// 生成配置文件
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());// 设置app 名字
appid.setText(appDTO.getAppid());// h5地址
apkname.setText(iosAppFileName);// 设置应用名
packagename.setText(package_name);// 设置包名
desc.setText("");//
downloadUrl.setText(downloadUrlStr);
// 设置IOS相关配置
client.setApkName(Cn2Spell.converterToSpell(appDTO.getName()));
client.setPackageName(package_name);
XMLWriter xmlWriter = null;
// 生成配置文件
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;
}
}
小伙伴们,我开了一家海淘护肤品淘宝店,搜索店铺“禾子蝶的海淘铺”,正品保证,欢迎进店选购哦。谢谢!
相关推荐
这是一个对于Java CountDownLatch的简单Demo CountDownLatch一个同步辅助类 在完成一组正在其他线程中执行的操作之前 它允许一个或多个线程一直等待 用给定的计数 初始化 CountDownLatch 由于调用了 countDown 方法 ...
利用 CountDownLatch 类实现线程同步,而不用回调机制。详见我的博文 http://blog.csdn.net/kroclin/article/details/37956949
主要为大家详细介绍了CountDownLatch的使用说明,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
具有countDownLatch的线程同步模式
3.2 使用CountDownLatch实现同步 主线程等待多个线程完成 4.1 场景介绍 4.2 使用CountDownLatch实现等待 CountDownLatch的其他应用场景 5.1 倒计时计时器 5.2 同时开始任务 5.3 等待多个资源就绪 CountDownLatch与...
CountDownLatch是一个同步工具类,它通过一个计数器来实现的,初始值为线程的数量。每当一个线程完成了自己的任务,计数器的值就相应得减1。当计数器到达0时,表示所有的线程都已执行完毕,然后在等待的线程就可以恢复...
在网上找的一个CountDownLatch的学习demo,感觉很不错,就摘抄过来了
主要介绍了如何使用CountDownLatch同步java多线程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Java中CountDownLatch进行多线程同步详解及实例代码的相关资料,需要的朋友可以参考下
《java并发编程》中CountDownLatch和CyclicBarrier用法实例大全,几乎包含了所有重要的用法
countdownlatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完毕再执行。从命名可以解读到countdown是倒数的意思,类似于我们倒计时的概念。 countdownlatch提供了两个方法,一个是...
java并发工具类(CountDownLatch+Semaphore+Exchanger);java并发工具类(CountDownLatch+Semaphore+Exchanger);java并发工具类(CountDownLatch+Semaphore+Exchanger);java并发工具类(CountDownLatch+...
递减锁存器CountDownLatch的使用以及注意事项!
目录 CountDownLatch是什么? CountDownLatch如何工作? 在实时系统中的应用场景 应用范例 常见的面试题 代码样例
mybaits 多线程 实现数据批量插入 (运用CountDownLatch实现闭锁) 1、mybatis批处理 2、数据分批量查询 3、数据分批量插入
本篇文章主要介绍了Java中多线程同步类 CountDownLatch的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧
CountDownLatch可以实现一个线程等待多个线程、多个线程等待一个线程、多个线程等待多个线程(这里不涉及)。 我们首先来看看怎么实现一个线程等待多个线程吧。 工厂中,对产品需要进行质检,5个工人进行检查,所有...
CountDownLatch与thread.join()的区别
CountDownLatch Demo
NULL 博文链接:https://cpjsjxy.iteye.com/blog/2272451