diff 比对 submit() 和 execute()方法的区别

阅读: 评论:0

diff 比对 submit() 和 execute()方法的区别

diff 比对 submit() 和 execute()方法的区别

/*** submit() 和 execute()方法的区别** execute() 参数 Runnable ;submit() 参数 (Runnable) 或 (Runnable 和 结果 T) 或 (Callable)** execute() 没有返回值;而 submit() 有返回值** submit() 的返回值 Future 调用get方法时,可以捕获处理异常*/public class Test {public  static int cut;static String s = "D:\java\Ai-effect-testing-platform-heads_feature_replay_0620\Ai-effect-testing-platform\1.txt";public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {FileOutputStream_FileInputStream_byte();}/*** 字节的形式写入文件,* 读取文件时先读取字节数组,* 再将字节数组转换为字符串形式*/private static void FileOutputStream_FileInputStream_byte() throws IOException {File file = new File(s);if(!ists()){ateNewFile();}FileOutputStream fos = new FileOutputStream(file);String ss = "zhan yuan5764764654654";fos.Bytes());fos.close();FileInputStream fis = new FileInputStream(file);byte[] bytes = new byte[ss.length()];while (ad(bytes,0,bytes.length)!=-1){System.out.println(new String(bytes));}}/*** 对文件内容进行整行读取* @throws IOException*/private static void BufferedWriter_BufferedReader_string() throws IOException {File file = new File(s);if(!ists()){//如果文件不存在,创建文件ateNewFile();}//创建BufferedWriter对象并向文件写入内容BufferedWriter bw = new BufferedWriter(new FileWriter(file));//向文件中写入内容bw.write("{n" +"    "code": 200,n" +"    "msg": "操作成功",n" +"    "data": [n" +"        {n" +"            "id": 87,n" +"            "type": "auth",n" +"            "name": "api网关鉴权",n" +"            "domain": "172.31.7.247:6060",n" +"            "room": "华为云",n" +"            "createTime": 1675842227000,n" +"            "updateTime": 1676447929000,n" +"            "creater": "clmeng",n" +"            "updater": "jingliu23"n" +"        },n" +"        {n" +"            "id": 77,n" +"            "type": "auth",n" +"            "name": "高防演练鉴权",n" +"            "domain": "/",n" +"            "room": "华为云",n" +"            "createTime": 1670310281000,n" +"            "updateTime": 1670329444000,n" +"            "creater": "clmeng",n" +"            "updater": "clmeng"n" +"        },n" +"        {n" +"            "id": 71,n" +"            "type": "auth",n" +"            "name": "混沌测试鉴权",n" +"            "domain": "/",n" +"            "room": "华为云",n" +"            "createTime": 1668476969000,n" +"            "updateTime": 1668564563000,n" +"            "creater": "clmeng",n" +"            "updater": "clmeng"n" +"        },n" +"        {n" +"            "id": 69,n" +"            "type": "auth",n" +"            "name": "AI诊断联调鉴权",n" +"            "domain": "/",n" +"            "room": "华为云",n" +"            "createTime": 1668077914000,n" +"            "updateTime": 1668077954000,n" +"            "creater": "clmeng",n" +"            "updater": "clmeng"n" +"        },n" +"        {n" +"            "id": 26,n" +"            "type": "auth",n" +"            "name": "测试环境token",n" +"            "domain": "/",n" +"            "room": "酒仙桥",n" +"            "createTime": 1656387522000,n" +"            "updateTime": 1661492485000,n" +"            "creater": "kunfang5",n" +"            "updater": "kunfang5"n" +"        },n" +"        {n" +"            "id": 33,n" +"            "type": "auth",n" +"            "name": "阿里云鉴权",n" +"            "domain": "/",n" +"            "room": "阿里云",n" +"            "createTime": 1656989654000,n" +"            "updateTime": 1656989797000,n" +"            "creater": "ganghuang4",n" +"            "updater": "yuanzhan"n" +"        },n" +"        {n" +"            "id": 29,n" +"            "type": "auth",n" +"            "name": "酒仙桥现网鉴权",n" +"            "domain": "/",n" +"            "room": "酒仙桥",n" +"            "createTime": 1656986644000,n" +"            "updateTime": 1656986644000,n" +"            "creater": "ganghuang4",n" +"            "updater": "ganghuang4"n" +"        }n" +"    ]n" +"}");bw.flush();bw.close();BufferedReader br = new BufferedReader(new FileReader(file));String line;while ((line &#adLine())!=null){System.out.println(line);}br.close();}/*** 对文件内容进行整行读取 char 个* @throws IOException*/private static void FileWriter_FileReader_char() throws IOException {File file = new File(s);if(!ists()){//如果文件不存在,创建文件ateNewFile();}FileWriter writer = new FileWriter(file);//向文件中写入内容writer.write("zhan yuan");writer.flush();writer.close();FileReader reader = new FileReader(file);//读取文件中的内容char[] ch = new char[9];ad(ch);for(char c: ch){System.out.println(c);}reader.close();}private static void fun2_6() throws InterruptedException {ExecutorService es = wFixedThreadPool(500);CountDownLatch latch = new CountDownLatch(300);Runnable task = ()->{for(int i =0;i<10;i++){System.out.println("cut =" + ++cut);}untDown();//等待其他线程处理完才继续当前线程};Long benin = System.currentTimeMillis();for(int i =0;i<300;i++){es.submit(task);}latch.await();Long end = System.currentTimeMillis();System.out.println("time = "+ (end-benin));println(cut);}private static void Spilp(){String data = "6&三-1号柜^7&三-2号柜^8&客服中心";List<Pair<String,String>> list = new ArrayList<>();String[] el = data.split("\^");for(String elme :el){String[] parts = elme.split("&");String id = parts[0];String name = parts[1];System.out.println(id);System.out.println(name);list.add(new Pair<>(id,name));}}private static void Schedule(){int cpuNubmer = Runtime().availableProcessors();System.out.println(cpuNubmer);ExecutorService executorService = wFixedThreadPool(cpuNubmer);for(int i = 1;i<10;i++){int fin = ute(()->{try {System.out.println(fin);Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}});}executorService.shutdown();}private static void Submit() throws ExecutionException, InterruptedException {ExecutorService executorService = wFixedThreadPool(3);for(int i = 1;i<10;i++){int fin = i;Future<?> sub = executorService.submit(()->{System.out.println(fin);try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}});System.out.println("result=" + ());//因为线程池代码没有运行异常,所以()每次都会返回null}executorService.shutdown();}
}
package com.zhixue.utils;import com.alibaba.dubbomon.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.difflib.DiffUtils;
import com.github.difflib.UnifiedDiffUtils;
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.patch.Patch;import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;public class StringDiffUtils {public static void main(String[] args) throws IOException, DiffException {String s1 ="121324";String s2 = "121324";//原始文件
//        Scanner sc = new Scanner(System.in);//        for(int i = 0;i<3;i++){List<String> targetS = Arrays.asList(s1);List<String> sourceS = Arrays.asList(s2);Patch<String> diff = DiffUtils.diff(targetS, sourceS);System.out.println(diff);for (AbstractDelta<String> delta : Deltas()) {System.out.println(delta);}
//        }//        List<String> original = adAllLines(new File("D:\java\Ai-effect-testing-platform-heads_feature_replay_0620\Ai-effect-testing-platform\1.txt").toPath());
//        //对比文件
//        List<String> revised = adAllLines(new File("D:\java\Ai-effect-testing-platform-heads_feature_replay_0620\Ai-effect-testing-platform\2.txt").toPath());
//
//        //两文件的不同点
//        Patch<String> patch = DiffUtils.diff(original, revised);
//        for (AbstractDelta<String> delta : Deltas()) {
//            System.out.println(delta);
//        }}
}
package com.zhixue.utils;
import com.github.difflib.UnifiedDiffUtils;
import com.github.difflib.algorithm.DiffException;
import com.github.difflib.patch.Patch;import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class DiffHandleUtils {public static void main(String[] args) throws DiffException {//对比 F:n1.txt和 F:n2.txt 两个文件,获得不同点List<String> diffString = DiffHandleUtils.diffString("D:\java\Ai-effect-testing-platform-heads_feature_replay_0620\Ai-effect-testing-platform\1.txt","D:\java\Ai-effect-testing-platform-heads_feature_replay_0620\Ai-effect-testing-platform\2.txt");//在F盘生成一个diff.html文件,打开便可看到两个文件的对比ateDiffHtml(diffString,"D:\java\Ai-effect-testing-platform-heads_feature_replay_0620\Ai-effect-testing-platform\diff.html");}/*** 对比两文件的差异,返回原始文件+diff格式** @param original 原文件内容* @param revised  对比文件内容*/public static List<String> diffString(List<String> original, List<String> revised) throws DiffException {return diffString(original, revised, null, null);}/*** 对比两文件的差异,返回原始文件+diff格式** @param original         原文件内容* @param revised          对比文件内容* @param originalFileName 原始文件名* @param revisedFileName  对比文件名*/public static List<String> diffString(List<String> original, List<String> revised, String originalFileName, String revisedFileName) throws DiffException {originalFileName = originalFileName == null ? "原始文件" : originalFileName;revisedFileName = revisedFileName == null ? "对比文件" : revisedFileName;//两文件的不同点Patch<String> patch = com.github.difflib.DiffUtils.diff(original, revised);//生成统一的差异格式List<String> unifiedDiff = ateUnifiedDiff(originalFileName, revisedFileName, original, patch, 0);int diffCount = unifiedDiff.size();if (unifiedDiff.size() == 0) {//如果两文件没差异则插入如下unifiedDiff.add("--- " + originalFileName);unifiedDiff.add("+++ " + revisedFileName);unifiedDiff.add("@@ -0,0 +0,0 @@");} else if (unifiedDiff.size() >= 3 && !(2).contains("@@ -1,")) {unifiedDiff.set(1, (1));//如果第一行没变化则插入@@ -0,0 +0,0 @@unifiedDiff.add(2, "@@ -0,0 +0,0 @@");}//原始文件中每行前加空格List<String> original1 = original.stream().map(v -> " " + v).List());//差异格式插入到原始文件中return insertOrig(original1, unifiedDiff);}/*** 对比两文件的差异,返回原始文件+diff格式** @param filePathOriginal 原文件路径* @param filePathRevised  对比文件路径*/public static List<String> diffString(String filePathOriginal, String filePathRevised) throws DiffException {//原始文件List<String> original = null;//对比文件List<String> revised = null;File originalFile = new File(filePathOriginal);File revisedFile = new File(filePathRevised);try {original = Path());revised = Path());} catch (IOException e) {e.printStackTrace();}return diffString(original, revised, Name(), Name());}/*** 通过两文件的差异diff生成 html文件,打开此 html文件便可看到文件对比的明细内容** @param diffString 调用上面 diffString方法获取到的对比结果* @param htmlPath   生成的html路径,如:/user/var/mbos/ent/21231/diff.html*/public static void generateDiffHtml(List<String> diffString, String htmlPath) {StringBuilder builder = new StringBuilder();for (String line : diffString) {builder.append(line);builder.append("n");}//如果打开html为空白界面,可能cdn加载githubCss失败 ,githubCss 链接可替换为 .js/10.7.1/styles/github.min.cssString githubCss = ".js/10.7.1/styles/github.min.css";//如果打开html为空白界面,可能cdn加载diff2htmlCss失败 ,diff2htmlCss 链接可替换为 .min.cssString diff2htmlCss = ".min.css";//如果打开html为空白界面,可能cdn加载diff2htmlJs失败, diff2htmlJs 链接可替换为 .min.jsString diff2htmlJs = ".min.js";//如果githubCss、diff2htmlCss、diff2htmlJs都加载失败可从  项目的resources目录下下载css和js手动引入到htmlString template = "<!DOCTYPE html>n" +"<html lang="en-us">n" +"  <head>n" +"    <meta charset="utf-8" />n" +"    <link rel="stylesheet" href="" + githubCss + "" />n" +"     <link rel="stylesheet" type="text/css" href="" + diff2htmlCss + "" />n" +"    <script type="text/javascript" src="" + diff2htmlJs + ""></script>n" +"  </head>n" +"  <script>n" +"    const diffString = `n" +"tempn" +"`;n" +"n" +"n" +"     document.addEventListener('DOMContentLoaded', function () {n" +"      var targetElement = ElementById('myDiffElement');n" +"      var configuration = {n" +"        drawFileList: true,n" +"        fileListToggle: true,n" +"        fileListStartVisible: true,n" +"        fileContentToggle: true,n" +"        matching: 'lines',n" +"        outputFormat: 'side-by-side',n" +"        synchronisedScroll: true,n" +"        highlight: true,n" +"        renderNothingWhenEmpty: true,n" +"      };n" +"      var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);n" +"      diff2htmlUi.draw();n" +"      diff2htmlUi.highlightCode();n" +"    });n" +"  </script>n" +"  <body>n" +"    <div id="myDiffElement"></div>n" +"  </body>n" +"</html>";template = place("temp", String());FileWriter f = null; //文件读取为字符流try {f = new FileWriter(htmlPath);BufferedWriter buf = new BufferedWriter(f); //文件加入缓冲区buf.write(template); //向缓冲区写入buf.close(); //关闭缓冲区并将信息写入文件f.close();} catch (IOException e) {e.printStackTrace();}}//统一差异格式插入到原始文件public static List<String> insertOrig(List<String> original, List<String> unifiedDiff) {List<String> result = new ArrayList<>();//unifiedDiff中根据@@分割成不同行,然后加入到diffList中List<List<String>> diffList = new ArrayList<>();List<String> d = new ArrayList<>();for (int i = 0; i < unifiedDiff.size(); i++) {String u = (i);if (u.startsWith("@@") && !"@@ -0,0 +0,0 @@".equals(u) && !u.contains("@@ -1,")) {List<String> twoList = new ArrayList<>();twoList.addAll(d);diffList.add(twoList);d.clear();d.add(u);continue;}if (i == unifiedDiff.size() - 1) {d.add(u);List<String> twoList = new ArrayList<>();twoList.addAll(d);diffList.add(twoList);d.clear();break;}d.add(u);}//将diffList和原始文件original插入到result,返回resultfor (int i = 0; i < diffList.size(); i++) {List<String> diff = (i);List<String> nexDiff = i == diffList.size() - 1 ? null : (i + 1);//含有@@的一行String simb = i == 0 ? (2) : (0);String nexSimb = nexDiff == null ? null : (0);//插入到resultinsert(result, diff);//解析含有@@的行,得到原文件从第几行开始改变,改变了多少(即增加和减少的行)Map<String, Integer> map = getRowMap(simb);if (null != nexSimb) {Map<String, Integer> nexMap = getRowMap(nexSimb);int start = 0;if (("orgRow") != 0) {start = ("orgRow") + ("orgDel") - 1;}int end = ("revRow") - 2;//插入不变的insert(result, getOrigList(original, start, end));}int start = (("orgRow") + ("orgDel") - 1);start = start == -1 ? 0 : start;if (ains("@@ -1,") && null == nexSimb && ("orgDel") != original.size()) {insert(result, getOrigList(original, start, original.size() - 1));} else if (null == nexSimb && (("orgRow") + ("orgDel") - 1) < original.size()) {insert(result, getOrigList(original, start, original.size() - 1));}}//如果你想知道两文件有几处不同可以放开下面5行代码注释,会在文件名后显示总的不同点有几处(即预览图中的xxx different),放开注释后有一个小缺点就是如果对比的是java、js等代码文件那代码里的关键字就不会高亮颜色显示,有一点不美观。//int diffCount = diffList.size() - 1;//if (!"@@ -0,0 +0,0 @@".(2))) {//    diffCount = diffList.size() > 1 ? diffList.size() : 1;//}//result.set(1, (1) + " ( " + diffCount + " different )");return result;}//将源文件中没变的内容插入resultpublic static void insert(List<String> result, List<String> noChangeContent) {for (String ins : noChangeContent) {result.add(ins);}}//解析含有@@的行得到修改的行号删除或新增了几行public static Map<String, Integer> getRowMap(String str) {Map<String, Integer> map = new HashMap<>();if (str.startsWith("@@")) {String[] sp = str.split(" ");String org = sp[1];String[] orgSp = org.split(",");//源文件要删除行的行号map.put("orgRow", Integer.valueOf(orgSp[0].substring(1)));//源文件删除的行数map.put("orgDel", Integer.valueOf(orgSp[1]));String[] revSp = org.split(",");//对比文件要增加行的行号map.put("revRow", Integer.valueOf(revSp[0].substring(1)));map.put("revAdd", Integer.valueOf(revSp[1]));}return map;}//从原文件中获取指定的部分行public static List<String> getOrigList(List<String> original1, int start, int end) {List<String> list = new ArrayList<>();if (original1.size() >= 1 && start <= end && end < original1.size()) {for (; start <= end; start++) {list.(start));}}return list;}
}
<dependency><groupId>io.github.java-diff-utils</groupId><artifactId>java-diff-utils</artifactId><version>4.5</version></dependency>

本文发布于:2024-02-04 08:57:11,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170703566254159.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:比对   区别   方法   diff   execute
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23