VB .NET 共享函数(如果同时调用多次)

发布于 2024-08-26 18:48:46 字数 823 浏览 6 评论 0原文

考虑我有一个共享函数:-

Public Shared Function CalculateAreaFromRadius(ByVal radius As Double) As Double

    ' square the radius...
    Dim radiusSquared As Double
    radiusSquared = radius * radius

    ' multiply it by pi...
    Dim result As Double
    result = radiusSquared * Math.PI

    'Wait a bit, for the sake of testing and 
    'simulate another call will be made b4 earlier one ended or such
     for i as Integer = 0 to integer.Max
     Next

    ' return the result...
    Return result

End Function

我的问题:

  1. 如果我在同一个 vb .net 应用程序中有两个或多个线程,并且每个线程都使用不同的 RADIUS 同时调用共享函数,那么它们每个线程都会获得自己的共享函数吗?区域?

  2. 我想知道对函数的每次调用是否使用相同的局部变量或每次调用都会创建局部变量的新实例?

  3. 如果我有多个(2+)单线程应用程序,并且它们都以不同的 RADIUS 值同时调用该函数,那么上述问题的答案是否相同?

我将感谢您的回复。谢谢。

Consider I have a shared function:-

Public Shared Function CalculateAreaFromRadius(ByVal radius As Double) As Double

    ' square the radius...
    Dim radiusSquared As Double
    radiusSquared = radius * radius

    ' multiply it by pi...
    Dim result As Double
    result = radiusSquared * Math.PI

    'Wait a bit, for the sake of testing and 
    'simulate another call will be made b4 earlier one ended or such
     for i as Integer = 0 to integer.Max
     Next

    ' return the result...
    Return result

End Function

My Questions:

  1. If I have two or more threads in the same vb .net app and each of them calls the shared function at the same time with different RADIUS, will they each get their own AREA?

  2. I want to know for each call to the function if it is using same local variables or each call creates new instances of local variables?

  3. Will the answers to above questions be same If I have multiple (2+) single threaded apps and they all call the function at the same time with different RADIUS value?

I will appreciate your reponse. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2024-09-02 18:48:46

1) 如果我在同一个 vb .net 应用程序中有两个或多个线程,并且每个线程都使用不同的 RADIUS 同时调用共享函数,那么它们每个线程都会获得自己的 AREA 吗?

是的,因为半径值是按值传递的,并且该方法只使用本地声明变量。

2)我想知道每次调用函数时是否使用相同的局部变量或每次调用都会创建局部变量的新实例?

每次调用都会创建其局部变量的一个新实例。

3) 如果我有多个 (2+) 单线程应用程序,并且它们都使用不同的 RADIUS 值同时调用该函数,那么上述问题的答案是否相同?

是的。同样,因为没有共享的信息存储,并且所有输入都是按值传递,所以它是线程安全的。

1) If I have two or more threads in the same vb .net app and each of them calls the shared function at the same time with different RADIUS, will they each get their own AREA?

Yes, because the radius value is passed by value and the method uses nothing but locally declare variables.

2) I want to know for each call to the function if it is using same local variables or each call creates new instances of local variables?

Each call creates a new instance of its local variables.

3) Will the answers to above questions be same If I have multiple (2+) single threaded apps and they all call the function at the same time with different RADIUS value?

Yes. Again, because there is no shared storage of information and because all inputs are passed by value, it is thread-safe.

快乐很简单 2024-09-02 18:48:46

该函数不使用外部状态。它仅访问其局部变量,因此从不同线程调用它是完全安全的。

  1. 是的,
  2. 局部变量特定于特定的调用,无论函数在哪个线程上运行(考虑递归函数;每次调用该函数时,它将有一组不同的局部变量)。
  3. 是的

The function uses no external state. It's only accessing its local variables so it's perfectly safe to call it from different threads.

  1. Yes
  2. Local variables are specific to the specific call regardless of the thread the function is running on (think about a recursive function; each time you call the function, it'll have a distinct set of local variables).
  3. Yes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文