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(!file.exists()){
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        String ss = "zhan yuan5764764654654";
        fos.write(ss.getBytes());
        fos.close();
        FileInputStream fis = new FileInputStream(file);
        byte[] bytes = new byte[ss.length()];
        while (fis.read(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(!file.exists()){//如果文件不存在,创建文件
            file.createNewFile();
        }
        //创建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\": \"http://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\": \"http://hwgf-aiplat.changyan.com/auth/v2/token/\",\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\": \"http://chaos-aiplat.changyan.com/auth/v2/token/\",\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\": \"http://prehwai.changyan.com/auth/\",\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\": \"http://test-aiplat.changyan.com/auth/\",\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\": \"http://xxzy-aiplat.changyan.com/auth/\",\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\": \"http://aiplat.changyan.com/auth/\",\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 =br.readLine())!=null){
            System.out.println(line);
        }
        br.close();

    }

    /**
     * 对文件内容进行整行读取 char 个
     * @throws IOException
     */
    private static void FileWriter_FileReader_char() throws IOException {
        File file = new File(s);
        if(!file.exists()){//如果文件不存在,创建文件
            file.createNewFile();
        }
        FileWriter writer = new FileWriter(file);
        //向文件中写入内容
        writer.write("zhan yuan");
        writer.flush();
        writer.close();
        FileReader reader = new FileReader(file);

        //读取文件中的内容
        char[] ch = new char[9];
        reader.read(ch);
        for(char c: ch){
            System.out.println(c);
        }
        reader.close();
    }
    private static void fun2_6() throws InterruptedException {
        ExecutorService es = Executors.newFixedThreadPool(500);
        CountDownLatch latch = new CountDownLatch(300);

        Runnable task = ()->{
            for(int i =0;i<10;i++){
                System.out.println("cut =" + ++cut);
            }
            latch.countDown();
            //等待其他线程处理完才继续当前线程
        };
        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));
        System.err.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.getRuntime().availableProcessors();
        System.out.println(cpuNubmer);
        ExecutorService executorService = Executors.newFixedThreadPool(cpuNubmer);
        for(int i = 1;i<10;i++){
            int fin = i;
            executorService.execute(()->{
                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 = Executors.newFixedThreadPool(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=" + sub.get());
            //因为线程池代码没有运行异常,所以sub.get()每次都会返回null
        }
        executorService.shutdown();
    }
}
package com.zhixue.utils;

import com.alibaba.dubbo.common.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 : diff.getDeltas()) {
                System.out.println(delta);
            }
//        }


//        List original = Files.readAllLines(new File("D:\\java\\Ai-effect-testing-platform-heads_feature_replay_0620\\Ai-effect-testing-platform\\1.txt").toPath());
//        //对比文件
//        List revised = Files.readAllLines(new File("D:\\java\\Ai-effect-testing-platform-heads_feature_replay_0620\\Ai-effect-testing-platform\\2.txt").toPath());
//
//        //两文件的不同点
//        Patch patch = DiffUtils.diff(original, revised);
//        for (AbstractDelta delta : patch.getDeltas()) {
//            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文件,打开便可看到两个文件的对比
        DiffHandleUtils.generateDiffHtml(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 = UnifiedDiffUtils.generateUnifiedDiff(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 && !unifiedDiff.get(2).contains("@@ -1,")) {
            unifiedDiff.set(1, unifiedDiff.get(1));
            //如果第一行没变化则插入@@ -0,0 +0,0 @@
            unifiedDiff.add(2, "@@ -0,0 +0,0 @@");
        }
        //原始文件中每行前加空格
        List<String> original1 = original.stream().map(v -> " " + v).collect(Collectors.toList());
        //差异格式插入到原始文件中
        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 = Files.readAllLines(originalFile.toPath());
            revised = Files.readAllLines(revisedFile.toPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return diffString(original, revised, originalFile.getName(), revisedFile.getName());
    }

    /**
     * 通过两文件的差异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 链接可替换为 https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css
        String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css";
        //如果打开html为空白界面,可能cdn加载diff2htmlCss失败 ,diff2htmlCss 链接可替换为 https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css
        String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css";
        //如果打开html为空白界面,可能cdn加载diff2htmlJs失败, diff2htmlJs 链接可替换为 https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js
        String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js";
        //如果githubCss、diff2htmlCss、diff2htmlJs都加载失败可从 https://github.com/1506085843/java-file-diff 项目的resources目录下下载css和js手动引入到html

        String template = "\n" +
                "\n" +
                "  \n" +
                "    \n" +
                "     + githubCss + "\" />\n" +
                "      + diff2htmlCss + "\" />\n" +
                "    \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "    
\n"
+ " \n" + ""; template = template.replace("temp", builder.toString()); 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 = unifiedDiff.get(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,返回result for (int i = 0; i < diffList.size(); i++) { List<String> diff = diffList.get(i); List<String> nexDiff = i == diffList.size() - 1 ? null : diffList.get(i + 1); //含有@@的一行 String simb = i == 0 ? diff.get(2) : diff.get(0); String nexSimb = nexDiff == null ? null : nexDiff.get(0); //插入到result insert(result, diff); //解析含有@@的行,得到原文件从第几行开始改变,改变了多少(即增加和减少的行) Map<String, Integer> map = getRowMap(simb); if (null != nexSimb) { Map<String, Integer> nexMap = getRowMap(nexSimb); int start = 0; if (map.get("orgRow") != 0) { start = map.get("orgRow") + map.get("orgDel") - 1; } int end = nexMap.get("revRow") - 2; //插入不变的 insert(result, getOrigList(original, start, end)); } int start = (map.get("orgRow") + map.get("orgDel") - 1); start = start == -1 ? 0 : start; if (simb.contains("@@ -1,") && null == nexSimb && map.get("orgDel") != original.size()) { insert(result, getOrigList(original, start, original.size() - 1)); } else if (null == nexSimb && (map.get("orgRow") + map.get("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 @@".equals(unifiedDiff.get(2))) { // diffCount = diffList.size() > 1 ? diffList.size() : 1; //} //result.set(1, result.get(1) + " ( " + diffCount + " different )"); return result; } //将源文件中没变的内容插入result public 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.add(original1.get(start)); } } return list; } }
<dependency>
            <groupId>io.github.java-diff-utils</groupId>
            <artifactId>java-diff-utils</artifactId>
            <version>4.5</version>
        </dependency>

你可能感兴趣的:(开发语言,java,后端)