多线程应用程序中的私有静态方法 [英] private static method in multi thread application

查看:86
本文介绍了多线程应用程序中的私有静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在开发多线程应用程序。在我的应用程序中,我使用一个类来处理一些计算。对于每个设备,我在线程中创建该类的一个单独实例。

性能是我开发的一部分。如果我使用私有静态方法进行某些计算,那么我可以提高一些性能。如果多个线程正在运行会产生什么影响。如果有多个类的实例,那些私有静态方法的范围是什么?

剂量范围仅适用于该类的实例?任何人都可以遇到或开发这种代码。



感谢您的帮助

Preetam Ramdhave

Hi All,
I am developing multi thread application. in my application i am using one class for processing some of the calculation. For each device i am creating one separate instance of that class in thread.
The performance is very imp part of my development. if i use private static method for some of the calculation then i can increase some performance . what will be the impact if multiple threads are running. if there are multiple instance of the class what will be the scope of those private static methods ?
Dose the scope remains only for that instance of the class ? can anyone encounter or developed such kind of code.

Thanks for your help
Preetam Ramdhave

推荐答案

如果你是创建类的实例然后你应该避免使用静态成员,但是如果你在静态方法中传递参数而不使用类中的任何局部变量那么它就不会产生任何交叉线程的问题。



以下链接可能会对您提供更多详细信息

http://stackoverflow.com/questions/1992475/how-to-write-correct-static-methods-multithread-safe [ ^ ]



http://stackoverflow.com/questions/1090650/are-static -methods-thread-safe [ ^ ]



http://social.msdn.microsoft .com /论坛/ en-US / csharplanguage / thread / c3cc953f-4659-44b2-8107-3e25d23fb02a / [ ^ ]
If you are creating instance of the class then you should avoid using static members, but if you are passing parameters in static method and not using any local variables from the class then it will not create any issue of cross thread.

Below link might help you more in detail
http://stackoverflow.com/questions/1992475/how-to-write-correct-static-methods-multithread-safe[^]

http://stackoverflow.com/questions/1090650/are-static-methods-thread-safe[^]

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c3cc953f-4659-44b2-8107-3e25d23fb02a/[^]



我做了一些阅读,却找不到任何东西。所以我写了一个程序来检查



Hi,
I have done some reading and was not able to find any thing. so i wrote one program to check that

class Test
  {
       int privVariable =10;
      public void GetInfo(int i)
      {
          privVariable = SetInformation(i);
      }
      private static int SetInformation(int SendValue)
      {
          return SendValue = SendValue * 22;
      }
  }





这段代码工作正常。

但是如果你编写私有静态方法然后它将适用于该类的所有类实例

所以答案是不是你应该小心使用静态方法你的多线程申请



问候

Preetam Ramdhave



this code works fine.
but in case if you write private static method then it will be accable for all the class instance of that class
so the answer is no you shoud carefully use static method in your multi thread application

Regards
Preetam Ramdhave


这篇关于多线程应用程序中的私有静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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