简体   繁体   English

Java,线程无法启动

[英]Java, Threads don't start

I don't understand why this only printing 2 actives threads. 我不明白为什么这只打印2个活动线程。

/* worker from the example */
for(int i = 0 ; i < 4 ; ++i){
    Worker worker = new Worker(articles, factory);
    worker.start();
    workers.add(worker);            
}       

System.out.println("Number of active threads : " + Worker.activeCount());

My Worker class is written likes this : 我的Worker类是这样写的:

class Worker extends Thread{}

Threads take time to start and execute. 线程需要一些时间来启动和执行。 The current thread doesn't stop and wait for them to start. 当前线程不会停止并等待它们启动。 This means you might not see any threads start by the time you print that message. 这意味着您在打印该消息时可能看不到任何线程启动。

Note: the whole purpose of threads is to run code independently as possible. 注意:线程的全部目的是尽可能独立地运行代码。 You can't make assumptions about the order in which things happen unless you have explicitly written code to guarantee this. 除非您已明确编写代码来保证这一点,否则您无法假设事情发生的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM