代码如下,在Main方法中我调用静态方法:
public class Main {\n public static void main(String[] args) {\n MyFactory.getSomething();\n System.out.println("over"); \n }\n}\nRun Code Online (Sandbox Code Playgroud)\n在静态方法中,让主线程等待,然后由demo线程唤醒。但是主线程没有唤醒,我不知道为什么。
\nimport java.util.concurrent.TimeUnit;\n\npublic class MyFactory {\n private static Object lock = new Object();\n\n static{\n init();\n }\n\n private static void init(){\n new Thread(()->{\n try {\n // make this thread run after main thread\n TimeUnit.SECONDS.sleep(3);\n System.out.println("task run...");\n synchronized (lock){\n lock.notifyAll(); // notify main thread\n }\n } catch (InterruptedException e) {\n throw new RuntimeException(e);\n }\n },"demo-thread").start();\n\n synchronized (lock){\n try {\n System.out.println("waiting...");\n …Run Code Online (Sandbox Code Playgroud)