当前位置: 首页 > news >正文

含关键词的网站建设青岛房产网官网网址

含关键词的网站建设,青岛房产网官网网址,制作网站背景怎么做,网站制作ppt模板最近#xff0c;我需要将一些文件#xff08;每个文件都有JSON格式的对象列表#xff08;数组#xff09;#xff09;转换为每个文件都具有相同数据#xff08;对象#xff09;的分隔行的文件。 这是一次性的任务#xff0c;很简单。 我使用Java nio的某些功能进行了读… 最近我需要将一些文件每个文件都有JSON格式的对象列表数组转换为每个文件都具有相同数据对象的分隔行的文件。 这是一次性的任务很简单。 我使用Java nio的某些功能进行了读写。 我以最简单的方式使用了GSON。 一个线程在文件上运行转换和写入。 整个操作在几秒钟内完成。 但是我想并发一点。 因此我增强了可同时工作的工具。 线程数 可运行以读取文件。 阅读器线程将提交到ExecutorService。 输出是对象列表示例中为User将被放入BlockingQueue。 可运行以写入文件。 每个可运行对象将从阻塞队列中轮询。 它将数据行写入文件。 我没有将编写器Runnable添加到ExecutorService而是仅使用它启动了一个线程。 Runnable具有while(some boolen is true) {...}模式。 有关以下内容的更多信息... 同步一切 BlockingQueue是这两种线程的接口。 由于writer的runnable在while循环消费者中运行我希望能够使其停止以便该工具终止。 因此我为此使用了两个对象 信号 读取输入文件的循环会增加一个计数器。 完成遍历输入文件并提交编写器后我在主线程中初始化了一个信号灯 semaphore.acquire(numberOfFiles); 在每个可运行的阅读器中我释放了信号量 semaphore.release(); 原子布尔 作者的while循环使用AtomicBoolean。 只要AtomicBoolean true编写器将继续。 在主线程中在获取信号量之后我将AtomicBoolean设置为false。 这使编写器线程可以终止。 使用Java NIO 为了扫描读取和写入文件系统我使用了Java NIO的某些功能。 扫描 Files.newDirectoryStream(inputFilesDirectory, *.json); 开始之前删除输出目录 Files.walkFileTree... BufferedReader和BufferedWriter Files.newBufferedReader(filePath); Files.newBufferedWriter(fileOutputPath, Charset.defaultCharset()); 一注。 为了生成此示例的随机文件我使用了apache commons lang RandomStringUtils.randomAlphabetic GitHub中的所有代码。 public class JsonArrayToJsonLines {private final static Path inputFilesDirectory Paths.get(src\\main\\resources\\files);private final static Path outputDirectory Paths.get(src\\main\\resources\\files\\output);private final static Gson gson new Gson();private final BlockingQueueEntitiesData entitiesQueue new LinkedBlockingQueue();private AtomicBoolean stillWorking new AtomicBoolean(true);private Semaphore semaphore new Semaphore(0);int numberOfFiles 0;private JsonArrayToJsonLines() {}public static void main(String[] args) throws IOException, InterruptedException {new JsonArrayToJsonLines().process();}private void process() throws IOException, InterruptedException {deleteFilesInOutputDir();final ExecutorService executorService createExecutorService();DirectoryStreamPath directoryStream Files.newDirectoryStream(inputFilesDirectory, *.json);for (int i 0; i 2; i) {new Thread(new JsonElementsFileWriter(stillWorking, semaphore, entitiesQueue)).start();}directoryStream.forEach(new ConsumerPath() {Overridepublic void accept(Path filePath) {numberOfFiles;executorService.submit(new OriginalFileReader(filePath, entitiesQueue));}});semaphore.acquire(numberOfFiles);stillWorking.set(false);shutDownExecutor(executorService);}private void deleteFilesInOutputDir() throws IOException {Files.walkFileTree(outputDirectory, new SimpleFileVisitorPath() {Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {Files.delete(file);return FileVisitResult.CONTINUE;}});}private ExecutorService createExecutorService() {int numberOfCpus Runtime.getRuntime().availableProcessors();return Executors.newFixedThreadPool(numberOfCpus);}private void shutDownExecutor(final ExecutorService executorService) {executorService.shutdown();try {if (!executorService.awaitTermination(120, TimeUnit.SECONDS)) {executorService.shutdownNow();}if (!executorService.awaitTermination(120, TimeUnit.SECONDS)) {}} catch (InterruptedException ex) {executorService.shutdownNow();Thread.currentThread().interrupt();}}private static final class OriginalFileReader implements Runnable {private final Path filePath;private final BlockingQueueEntitiesData entitiesQueue;private OriginalFileReader(Path filePath, BlockingQueueEntitiesData entitiesQueue) {this.filePath filePath;this.entitiesQueue entitiesQueue;}Overridepublic void run() {Path fileName filePath.getFileName();try {BufferedReader br Files.newBufferedReader(filePath);User[] entities gson.fromJson(br, User[].class);System.out.println(--- fileName);entitiesQueue.put(new EntitiesData(fileName.toString(), entities));} catch (IOException | InterruptedException e) {throw new RuntimeException(filePath.toString(), e);}}}private static final class JsonElementsFileWriter implements Runnable {private final BlockingQueueEntitiesData entitiesQueue;private final AtomicBoolean stillWorking;private final Semaphore semaphore;private JsonElementsFileWriter(AtomicBoolean stillWorking, Semaphore semaphore,BlockingQueueEntitiesData entitiesQueue) {this.stillWorking stillWorking;this.semaphore semaphore;this.entitiesQueue entitiesQueue;}Overridepublic void run() {while (stillWorking.get()) {try {EntitiesData data entitiesQueue.poll(100, TimeUnit.MILLISECONDS);if (data ! null) {try {String fileOutput outputDirectory.toString() File.separator data.fileName;Path fileOutputPath Paths.get(fileOutput);BufferedWriter writer Files.newBufferedWriter(fileOutputPath, Charset.defaultCharset());for (User user : data.entities) {writer.append(gson.toJson(user));writer.newLine();}writer.flush();System.out.println( data.fileName);} catch (IOException e) {throw new RuntimeException(data.fileName, e);} finally {semaphore.release();}}} catch (InterruptedException e1) {}}}}private static final class EntitiesData {private final String fileName;private final User[] entities;private EntitiesData(String fileName, User[] entities) {this.fileName fileName;this.entities entities;}} }翻译自: https://www.javacodegeeks.com/2014/12/playing-with-java-concurrency.html
http://www.pierceye.com/news/461287/

相关文章:

  • 深圳网站外包公司注册公司流程和费用找哪家
  • 专业新站整站快速排名公司商业网站和企业网站的区别
  • 网站做等保备案河北建设厅网站登陆怎么找附件
  • 网站前台显示数据库指定分类怎么做php优化大师官方免费
  • 用ps软件做ppt模板下载网站c2c网站开发策划
  • 标志空间 网站tk域名网站多少
  • dedecms网站地图插件永康公司网站开发
  • 比较网站建设有没有学做ppt发网站或论坛
  • 用asp做网站流程做科研找论文的网站
  • 新浪网站怎么做推广广告网站模板下载不了
  • 微网站建设哪家优惠h5小游戏在线玩
  • 娄底高端网站建设网站建设费计入 科目
  • 免费企业网站程序上传wordpress 卸载
  • 大庆市建设局网站上不去linux删除WordPress
  • 宣城市建设监督管理局网站下载怎么上wordpress
  • 福州做网站fjfzwl编写软件开发文档
  • 平台设计网站公司电话号码建站哪家好用兴田德润
  • 宝安网站建设信科免费网站开发 自动填写表单
  • 网站怎么更新文章动漫网站在线免费观看
  • 织梦 网站迁移网页制作三剑客通常指
  • 南京本地网站建站武安百度seo
  • 特定ip段访问网站代码西安免费建网站设计
  • 个人网站备案取消wordpress可以做大吗
  • 如何做网站管理网站服务器基本配置
  • 做网站需要参考书目书龙岩营销型网站建设
  • 南通网站建设解决方案求助如何做网站推广
  • 揭阳企业做网站淮安做网站
  • 怎么给餐饮店做网站用织梦做企业网站
  • 技术支持 创思佳网站建设如何制作自己的网站
  • 济南网站建设公司晟创未来wordpress xml插件