线程开销开销、线程

2023-09-06 15:57:28 作者:丑的让麻麻放心

我需要线程一些澄清。 当线程1 MB的内存开销要求? 难道在创建线程对象(costructor)或开始()方法被调用时? 是否立即启动方法返回(线程真正启动前)?

如果我有一个创建并启动N个线程>线程池的maxThreads,一个循环将循环完整的快,否则会等到所有线程的创建和启动?

感谢您

解决方案   

时它创建线程对象时(costructor),或在启动()方法被调用?

在1MB(默认,但可配置的,因为它的线程的堆栈大小)的线程实际启动时创建的。

  

请问Start方法将立即返回(线程真正启动前)?

开始()方法是异步的,并立即返回。 Thread.Start 的文档指定它会导致一个线程是计划执行。

  线程

如果我有一个创建并启动N个线程>线程池的maxThreads,一个循环将循环中的完整的快,否则将等待被创建并启动所有的线程?

这将快速完成(或者,至少,没有等待的线程启动)。

I need some clarification on threads. When is the 1 MB memory overhead for threads claimed? Is it when the thread object is created (costructor) or when the Start() method is called? Does the Start method returns immediately (before the thread really starts)?

If I have a loop that creates and starts N threads > maxThreads of ThreadPool, will the loop complete fast or it will wait till all threads are created and started?

Thank You

解决方案

Is it when the thread object is created (costructor) or when the Start() method is called?

The 1mb (by default, but configurable, as it's the stack size of the thread) is created when the thread actually starts.

Does the Start method returns immediately (before the thread really starts)?

The Start() method is asynchronous, and returns immediately. Thread.Start's documentation specifies that it "causes a thread to be scheduled for execution."

If I have a loop that creates and starts N threads > maxThreads of ThreadPool, will the loop the complete fast or it will wait to be created and started all the threads?

It will complete quickly (or, at least, not wait for the threads to start up).