是MVC4行动是线程安全? [英] Is MVC4 Action Is Thread Safely?

查看:99
本文介绍了是MVC4行动是线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net mvc4,但我有一个关于行动的问题,是行动是线程安全的?我的意思是,当2请求来了,这是可能的1请求处理,但没有完成,第二个请求开始办理?

,或者如果它是线程安全的,等待完成一个请求,另一个请求?

感谢。


  

UPDATE1:


  
  

看起来像它不是线程安全的。我有一个控制器的动作。在操作5方法已经执行。 (1,2,3,4,5)。有3请求走到一起(A,B,C)。


  
  

还有就是我的日志显示,这些执行命令:


  
  

    

A-1 B-1 C-1 A-2 A-3 B-2 B-3 B-4 B-5 C-4 C-5 A-2 A-3 A-4 A-5


  
  
  

由于我需要检查的方法1的数据,如果没有找到的数据,我会
  把它写在方法5.如果执行所不使用线程安全的,它会写3
  数据。(因为方法1中的所有未找到数据,也都迫不及待的完成
  在一起......)


  
  

UPDATE2:


  
  

也许我混淆有关线程安全的,但感谢网友帮助。



解决方案

  

这是可能的1请求处理,但没有完成,第二个请求启动手柄?


当然,否则你的网站能有效地只处理一次一个页面视图。

您将不得不作出的您的code 线程安全的。例如,见如何锁定一个asp.net mvc的动作?,或者你可以例如在数据库中创建,你的code的查询锁定。

I'm using asp.net mvc4, but I has a question about Action, Is the Action is Thread safely? I mean when 2 request is coming , Is that possible 1 request is handle but not finish, second request start handle?

Or if It is thread safely, wait for finish one request and another request?

Thanks.

Update1:

Looks like it's not thread safe. I have a action on Controller. in the action 5 method has been execute. (1,2,3,4,5). there is 3 request come together(A, B,C).

there is my log display, these execute order:

A-1 B-1 C-1 A-2 A-3 B-2 B-3 B-4 B-5 C-4 C-5 A-2 A-3 A-4 A-5

Because I need check data on 1 method, if the data not found, I will write it on method 5. If execute not use thread safe, it will write 3 data.(because all not found data on method 1, also all not wait finish together......)

Update2:

Maybe I'm confuse about Thread safe, but Thanks friends helping.

解决方案

Is that possible 1 request is handle but not finish, second request start handle?

Of course, otherwise your website could effectively handle only one page view at a time.

You will have to make your code thread-safe. See for example how to lock an asp.net mvc action?, or you could for example create a lock in your database which your code has to query.

这篇关于是MVC4行动是线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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