Are there any cases when it's preferable to use a plain old Thread object instead of one of the newer constructs?

前端 未结 10 1982
挽巷
挽巷 2021-01-30 01:45

I see a lot of people in blog posts and here on SO either avoiding or advising against the usage of the Thread class in recent versions of C# (and I mean of course

相关标签:
10条回答
  • 2021-01-30 02:36

    Task and Thread are different abstractions. If you want to model a thread, the Thread class is still the most appropriate choice. E.g. if you need to interact with the current thread, I don't see any better types for this.

    However, as you point out .NET has added several dedicated abstractions which are preferable over Thread in many cases.

    0 讨论(0)
  • 2021-01-30 02:45

    The Thread class is not obsolete, it is still useful in special circumstances.

    Where I work we wrote a 'background processor' as part of a content management system: a Windows service that monitors directories, e-mail addresses and RSS feeds, and every time something new shows up execute a task on it - typically to import the data.

    Attempts to use the thread pool for this did not work: it tries to execute too much stuff at the same time and trash the disks, so we implemented our own polling and execution system using directly the Thread class.

    0 讨论(0)
  • 2021-01-30 02:46

    Threads are a basic building block for certain things (namely parallelism and asynchrony) and thus should not be taken away. However, for most people and most use cases there are more appropriate things to use which you mentioned, such as thread pools (which provide a nice way of handling many small jobs in parallel without overloading the machine by spawning 2000 threads at once), BackgroundWorker (which encapsulates useful events for a single shortlived piece of work).

    But just because in many cases those are more appropriate as they shield the programmer from needlessly reinventing the wheel, doing stupid mistakes and the like, that does not mean that the Thread class is obsolete. It is still used by the abstractions named above and you would still need it if you need fine-grained control over threads that is not covered by the more special classes.

    In a similar vein, .NET doesn't forbid the use of arrays, despite List<T> being a better fit for many cases where people use arrays. Simply because you may still want to build things that are not covered by the standard lib.

    0 讨论(0)
  • 2021-01-30 02:47

    It's not definitely obsolete.

    The problem with multithreaded apps is that they are very hard to get right (often indeterministic behavior, input, output and also internal state is important), so a programmer should push as much work as possible to framework/tools. Abstract it away. But, the mortal enemy of abstraction is performance.

    So my question is, are there any cases when it's necessary or useful to use a plain old Thread object instead of one of the above constructs?

    I'd go with Threads and locks only if there will be serious performance problems, high performance goals.

    0 讨论(0)
提交回复
热议问题