NET开发人员真正需要的是线程安全关注

NET开发人员真正需要的是线程安全关注

本文介绍了难道ASP.NET开发人员真正需要的是线程安全关注?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为自己知道线程的概念,为什么某些code是或不是线程安全的,但谁的人主要是与ASP.NET工作,线程和线程安全的东西我很少想关于。但是,我似乎在众多的评论和答复运行(不一定是ASP.NET)对堆栈溢出到警告的效果 - !这不是线程安全 ,它往往让我揣摩是否我写类似code,实际上可以在我的应用程序出现问题了。 [震撼,恐怖等]所以我不禁要问:

I consider myself aware of the concepts of threading and why certain code is or isn’t "thread-safe," but as someone who primarily works with ASP.NET, threading and thread safety is something I rarely think about. However, I seem to run across numerous comments and answers (not necessarily for ASP.NET) on Stack Overflow to the effect of "warning – that’s not thread-safe!," and it tends to make me second guess whether I’ve written similar code that actually could cause a problem in my applications. [shock, horror, etc.] So I’m compelled to ask:

不要ASP.NET开发人员的真正的需要与线程安全的关注?

Do ASP.NET developers really need to be concerned with thread safety?

我的看法:虽然Web应用程序本质上是多线程的,每一个特定的请求是在一个线程,和所有的非静态的类型可以创建,修改或破坏是专用于该单线程/请求。如果请求创建它创建一个业务对象和实例I要偷懒初始化该对象内集合,的不要紧的,如果它不是线程安全的,因为它永远不会被另一个线程感动。 ...对? (假设我不是开始一个新的线程来揭开序幕长期运行的要求在异步过程中,我清楚地知道,改变了一切。)

My Take: While a web application is inherently multi-threaded, each particular request comes in on a single thread, and all non-static types you create, modify, or destroy are exclusive to that single thread/request. If the request creates an instance of a DAL object which creates an instance of a business object and I want to lazy-initialize a collection within this object, it doesn’t matter if it’s not thread-safe, because it will never be touched by another thread. ...Right? (Let’s assume I’m not starting a new thread to kick off a long-running asynchronous process during the request. I’m well aware that changes everything.)

当然,静态类,方法和变量是正好相反。他们被的每个的请求共享,开发人员必须非常小心,不要有不安全code,当一个用户执行的,可以对所有其他的意想不到的效果。

Of course, static classes, methods and variables are just the opposite. They are shared by every request, and the developer must be very careful not to have "unsafe" code that when executed by one user, can have an unintended effect on all others.

但仅此而已,因此螺纹ASP.NET安全主要归结为:小心你如何设计和使用静态。除此之外,你不必担心它更在所有。

But that’s about it, and thus thread safety in ASP.NET mostly boils down to this: Be careful how you design and use statics. Other than that, you don’t need to worry about it much at all.

我错了任何有关这?难道你不同意?赐教!

Am I wrong about any of this? Do you disagree? Enlighten me!

推荐答案

有除了那些在所有请求发送到应用程序共享的静态项目的特定对象。小心将项目中的应用程序缓存是不是线程安全的,例如。此外,没有什么prevents从产卵你自己的线程进行后台处理,而处理的请求。

There are certain objects in addition to static items that are shared across all requests to an application. Be careful about putting items in the application cache that are not thread-safe, for example. Also, nothing prevents you from spawning your own threads for background processing while handling a request.

这篇关于难道ASP.NET开发人员真正需要的是线程安全关注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:46