1、文档



2、相关问题


2.1、两个list集合的交集、差集、并集

  • 描述:

list1集合和list2集合的交集、差集、并集
解决同步问题

  • 解决方案:

问题的解决方案

  1. @org.junit.Test
  2. public void test(){
  3. List<String> list1 = new ArrayList<String>();
  4. list1.add("1");
  5. list1.add("2");
  6. list1.add("3");
  7. list1.add("5");
  8. list1.add("6");
  9. List<String> list2 = new ArrayList<String>();
  10. list2.add("2");
  11. list2.add("3");
  12. list2.add("7");
  13. list2.add("8");
  14. // 交集
  15. List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
  16. System.out.println("---交集 intersection---");
  17. intersection.parallelStream().forEach(System.out :: println);
  18. // 差集 (list1 - list2)
  19. List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
  20. System.out.println("---差集 reduce1 (list1 - list2)---");
  21. reduce1.parallelStream().forEach(System.out :: println);
  22. // 差集 (list2 - list1)
  23. List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
  24. System.out.println("---差集 reduce2 (list2 - list1)---");
  25. reduce2.parallelStream().forEach(System.out :: println);
  26. // 并集
  27. List<String> listAll = list1.parallelStream().collect(toList());
  28. List<String> listAll2 = list2.parallelStream().collect(toList());
  29. listAll.addAll(listAll2);
  30. System.out.println("---并集 listAll---");
  31. listAll.parallelStream().forEachOrdered(System.out :: println);
  32. // 去重并集
  33. List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
  34. System.out.println("---得到去重并集 listAllDistinct---");
  35. listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
  36. System.out.println("---原来的List1---");
  37. list1.parallelStream().forEachOrdered(System.out :: println);
  38. System.out.println("---原来的List2---");
  39. list2.parallelStream().forEachOrdered(System.out :: println);
  40. }

2.2、ThreadPoolExecutor线程池使用

  • 描述:

LinkedBlockingQueue 默认无界

  • 解决方案:

    1. @Slf4j
    2. public class Test {
    3. private int count = 6;
    4. private CountDownLatch countDownLatch = new CountDownLatch(count);
    5. @org.junit.Test
    6. public void test() throws InterruptedException {
    7. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10));
    8. for (int i = 0; i < count; i++) {
    9. threadPoolExecutor.execute(new Runnable() {
    10. @Override
    11. public void run() {
    12. log.info("===============");
    13. try {
    14. countDownLatch.await();
    15. } catch (InterruptedException e) {
    16. e.printStackTrace();
    17. }
    18. }
    19. });
    20. countDownLatch.countDown();
    21. }
    22. System.out.println("end");
    23. Thread.sleep(3000);
    24. }
    25. public static void main(String[] args) throws InterruptedException, UnknownHostException {
    26. }
    27. }

2.3、命令行编译打包

  • 描述:

通过jdk的工具使用命令来进行java项目的编译打包

  • 解决方案:

编译

  1. # 将class文件生成到指定的targer目录
  2. javac src/com/demo/Hello.java -d target

打包

  1. # -C target . 参数,指定把 target 目录下的所有文件打包。注意,需要从包名开始打包
  2. jar -cvf my.jar -C target .
  3. # 指定com目录下所有class文件打包,报名为my.jar
  4. jar -cvf my.jar -C target com

添加主类:
打包好后现在还不能执行,需要添加主类;

  1. jar中打开META-INF/MANIFEST.MF 指定主类
  2. Main-Class: com.demo.Hello

执行

  1. java -jar my.jar

2.4、打包

2.4.1、不指定主类(maven)

2.4.1.1、不带依赖

  1. <build>
  2. <finalName>demo</finalName>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-compiler-plugin</artifactId>
  7. <configuration>
  8. <encoding>UTF-8</encoding>
  9. <source>8</source>
  10. <target>8</target>
  11. </configuration>
  12. </plugin>
  13. </plugins>
  14. </build>

2.4.1.2、带依赖

  1. <build>
  2. <finalName>${name}-${version}</finalName>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-compiler-plugin</artifactId>
  7. <configuration>
  8. <encoding>UTF-8</encoding>
  9. <source>8</source>
  10. <target>8</target>
  11. </configuration>
  12. </plugin>
  13. <plugin>
  14. <artifactId>maven-assembly-plugin</artifactId>
  15. <configuration>
  16. <descriptorRefs>
  17. <descriptorRef>jar-with-dependencies</descriptorRef>
  18. </descriptorRefs>
  19. </configuration>
  20. <executions>
  21. <execution>
  22. <id>make-assembly</id> <!-- this is used for inheritance merges -->
  23. <phase>package</phase> <!-- bind to the packaging phase -->
  24. <goals>
  25. <goal>single</goal>
  26. </goals>
  27. </execution>
  28. </executions>
  29. </plugin>
  30. </plugins>
  31. </build>

2.4.2、指定主类(maven)

2.4.2.1、不带依赖

  1. <build>
  2. <finalName>demo</finalName>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-compiler-plugin</artifactId>
  7. <configuration>
  8. <encoding>UTF-8</encoding>
  9. <source>8</source>
  10. <target>8</target>
  11. </configuration>
  12. </plugin>
  13. </plugins>
  14. </build>

手动在jar中MANIFEST.MF中加入Main-Class:属性,指定主类
image.png

2.4.2.2、带依赖

  1. <build>
  2. <finalName>${name}-${version}</finalName>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-compiler-plugin</artifactId>
  7. <configuration>
  8. <encoding>UTF-8</encoding>
  9. <source>8</source>
  10. <target>8</target>
  11. </configuration>
  12. </plugin>
  13. <plugin>
  14. <artifactId>maven-assembly-plugin</artifactId>
  15. <configuration>
  16. <archive>
  17. <manifest>
  18. <mainClass>com.lms.maven.jar.Test2</mainClass>
  19. </manifest>
  20. </archive>
  21. <descriptorRefs>
  22. <descriptorRef>jar-with-dependencies</descriptorRef>
  23. </descriptorRefs>
  24. </configuration>
  25. <executions>
  26. <execution>
  27. <id>make-assembly</id> <!-- this is used for inheritance merges -->
  28. <phase>package</phase> <!-- bind to the packaging phase -->
  29. <goals>
  30. <goal>single</goal>
  31. </goals>
  32. </execution>
  33. </executions>
  34. </plugin>
  35. </plugins>
  36. </build>

2.4.2.4、带外部依赖(lib/)

第一种:把lib加入库,添加如下就可以使用,不需要添加依赖坐标

  1. <build>
  2. <resources>
  3. <resource>
  4. <directory>lib</directory>
  5. <targetPath>/BOOT-INF/lib/</targetPath>
  6. <includes>
  7. <include>**/*.jar</include>
  8. </includes>
  9. </resource>
  10. </resources>
  11. <plugins>
  12. <plugin>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-maven-plugin</artifactId>
  15. <version>2.1.18.RELEASE</version>
  16. <executions>
  17. <execution>
  18. <goals>
  19. <goal>repackage</goal>
  20. </goals>
  21. </execution>
  22. </executions>
  23. </plugin>
  24. </plugins>
  25. </build>

第二种:需要添加引用
image.png

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <version>2.1.18.RELEASE</version>
  7. <configuration>
  8. <!-- 打包本地库-->
  9. <includeSystemScope>true</includeSystemScope>
  10. </configuration>
  11. <executions>
  12. <execution>
  13. <goals>
  14. <goal>repackage</goal>
  15. </goals>
  16. </execution>
  17. </executions>
  18. </plugin>
  19. </plugins>
  20. </build>

2.5、maven打包时给jar包起一个固定的名字

  1. <build>
  2. <finalName>${parent.artifactId}</finalName>
  3. </build>

2.6、Error attempting to get column ‘create_time’ from result set.

Error attempting to get column ‘create_time’ from result set. Cause: java.sql.SQLFeatureNotSupported

https://blog.csdn.net/weixin_39520967/article/details/99706639
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
druid的版本升级到1.1.18以上就可

2.7、获取打jar包后的资源文件路径

jar包执行时访问资源文件造成的FileSystemNotFoundException问题
使用

  1. properties = new Properties();
  2. URL url = DmdbSqlConfig.class.getClassLoader().getResource("dmsql");
  3. if (url != null) {
  4. Path path = Paths.get(url.toURI());
  5. Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
  6. @Override
  7. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  8. log.info("加载sql文件:{}", file);
  9. properties.load(new FileInputStream(new File(file.toUri())));
  10. return super.visitFile(file, attrs);
  11. }
  12. });
  13. }

在idea中使用没有问题,但是打成jar包 在服务器上执行的时候会出现异常,
解决办法: 通过spring注解来解决

  1. @Value("classpath:/dmsql/**")
  2. private Resource[] sqlResources;
  3. public void loadSql() {
  4. Stream.of(sqlResources).forEach(sqlResources -> {
  5. try (InputStream is = sqlResources.getInputStream()) {
  6. log.info("加载sql文件:{}", sqlResources.getFilename());
  7. properties.load(is);
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. throw new RuntimeException("导入sql语句异常" + e.getMessage());
  11. }
  12. });
  13. }
  14. 或手动注入
  15. ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
  16. Resource[] resources = resolver.getResources("classpath:/dmsql/*/*");
  17. for (Resource resource : resources) {
  18. System.out.println(resource);
  19. }

2.8、线程诊断cpu占用过高

1、先用top命令查看哪一个java程序占用cpu过高
2、再用**ps H -eo pid,tid,%cpu | grep 进程id ** 查看哪一个线程占用cpu过高
3、将线程id换算成16进制
4、使用**jstack 进程id**查看所有的java线程,找到具体的线程,根据换算来的id查找
image.png


2.9、程序结束前动作

  1. Runtime runtime = Runtime.getRuntime();
  2. runtime.addShutdownHook(new Thread(() -> System.out.println("程序退出前动作")));

注意:这个适合在正常关闭程序才会执行,断电,直接杀死进程是不会执行的

2.10、指定编码去读取properties文件

  1. properties = new Properties();
  2. properties.load(new InputStreamReader(is, StandardCharsets.UTF_8));
  3. // 或者在取值的时候手动转换编码:new String(value.getBytes("ISO-8859-1"),"UTF-8");