在主线程以外的其他线程中启动多个线程 [英] starting multiple threads inside a thread other than main thread

查看:226
本文介绍了在主线程以外的其他线程中启动多个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

在另一个线程(T1)(主线程M1除外)中启动一个线程(T2)是一种好习惯吗?

如果我们进行这种设计,我将面临哪些实际问题?

问候,

Dear All,

Is it a good practice to start a thread (T2) inside another thread (T1) (other than main thread M1)?

What practical issues i will face if we go for this kind of design?

Regards,

推荐答案

我没有看到与此选择有关的直接问题.但是,您至少有两个地方可以启动线程(并可能停止).我认为,这种额外的复杂性应通过总体设计上的改进来证明.
我的两分钱.
I don''t see direct issues related with this choice. However you have at least two places where threads start (and possibly stop). This additional complexity, in my opinion, should be justified by an overall improvement of your design.
My two cents.


主线程和其他线程之间几乎没有区别.严格来说,没有"main"这样的线程,它只是一些初始线程,即执行入口点的那个线程.这种设计不会增加任何麻烦或问题.只有一件事:每个线程都应达到某种目的,这应该证明其使用合理性并为存在而花费资源.当然,如果一个线程的纯粹目的是创建和/或启动某个其他线程,这几乎不是一个好的设计,但是如果该线程具有某些独特的功能,为什么不呢?

—SA
There is practically no difference between main thread and other threads. Strictly speaking, there is not such thread as "main", this is merely some initial thread, the one where the entry point is executed. This kind of design cannot add any hassles or problems. There is only one thing: each thread should serve some purpose which should justify its use and spending resources for it existence. Of course, if a sheer purpose of a thread is to create and/or start some other thread, this hardly could be a good design, but if this thread carries some distinct functionality, why not?

—SA


您可以在需要它们的任何时候启动线程,而诀窍和问题是资源争用.

如果可以使用.net 4,请尝试使用Task s代替线程.
You can start thread any time you need them, the trick and problem is resource contention.

If you can use .net 4 try Tasks instead of threads.


这篇关于在主线程以外的其他线程中启动多个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆