小编Q10*_*ing的帖子

为什么主线程没有唤醒?

代码如下,在Main方法中我调用静态方法:

\n
public class Main {\n    public static void main(String[] args) {\n        MyFactory.getSomething();\n        System.out.println("over"); \n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在静态方法中,让主线程等待,然后由demo线程唤醒。但是主线程没有唤醒,我不知道为什么。

\n
import 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)

java multithreading jvm classloader

5
推荐指数
1
解决办法
191
查看次数

标签 统计

classloader ×1

java ×1

jvm ×1

multithreading ×1