From 453c52987ec942382692b32ee256ec9e24dee4a6 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Sat, 13 Feb 2021 18:03:11 +0800 Subject: [PATCH] Optimized code --- .../java/io/github/ehlxr/extension/SPI.java | 2 -- .../java/io/github/ehlxr/thread/Main.java | 34 ++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/github/ehlxr/extension/SPI.java b/src/main/java/io/github/ehlxr/extension/SPI.java index 69fdfb5..e88ece2 100644 --- a/src/main/java/io/github/ehlxr/extension/SPI.java +++ b/src/main/java/io/github/ehlxr/extension/SPI.java @@ -24,8 +24,6 @@ package io.github.ehlxr.extension; -import io.github.ehlxr.did.extension.ExtensionLoader; - import java.lang.annotation.*; /** diff --git a/src/main/java/io/github/ehlxr/thread/Main.java b/src/main/java/io/github/ehlxr/thread/Main.java index 961749b..b282096 100644 --- a/src/main/java/io/github/ehlxr/thread/Main.java +++ b/src/main/java/io/github/ehlxr/thread/Main.java @@ -31,27 +31,37 @@ package io.github.ehlxr.thread; public class Main { private static volatile boolean flag = false; + /** + * 1、子线程阻塞主线程将会结束 + * 2、JVM 会等待待所有子线程结束 + *

+ * When a Java Virtual Machine starts up, there is usually a single + * non-daemon thread (which typically calls the method named main of some + * designated class). The Java Virtual Machine continues to execute + * threads until either of the following occurs: + *

+ * The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. + * All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception + * that propagates beyond the run method. + */ + @SuppressWarnings("AlibabaAvoidManuallyCreateThread") public static void main(String[] args) throws InterruptedException { new Thread(() -> { System.out.println("wating data...." + Thread.currentThread().getName()); - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - e.printStackTrace(); + while (!flag) { } - System.out.println("complete!" + Thread.currentThread().getName()); }).start(); - // Thread.sleep(2000); + Thread.sleep(2000); - // new Thread(() -> { - // System.out.println("prepare data..." + Thread.currentThread().getName()); - // flag = true; - // - // System.out.println("prepare data end..." + Thread.currentThread().getName()); - // }).start(); + new Thread(() -> { + System.out.println("prepare data..." + Thread.currentThread().getName()); + flag = true; + + System.out.println("prepare data end..." + Thread.currentThread().getName()); + }).start(); } }