VB.NET共享函数,如果多次同时调用 [英] VB .NET Shared Function if called multiple times simultaneously

查看:479
本文介绍了VB.NET共享函数,如果多次同时调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我有一个共享函数:
$ b $ pre $ Public Shared Function CalculateAreaFromRadius(ByVal radius As Double)As Double

'平方半径...
Dim radiusSquared As Double
radiusSquared =半径*半径

'将其乘以pi ...
Dim结果作为Double
结果= radiusSquared * Math.PI

'稍等一下,为了测试和
'模拟另一个调用将在b4早些时候结束或者这样做
for i as Integer = 0 to integer.Max
Next

'返回结果...
返回结果

End Function

我的问题:

    $ b $如果我在同一个vb.net应用程序中有两个或多个线程,并且每个线程都使用不同的RADIUS同时调用共享函数,它们是否都会获得它们自己的AREA?
  1. 我想知道对函数的每次调用是否使用了相同的局部变量,或者每个调用都创建了局部变量的新实例?


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


我会感谢您的回应。

解决方案


1)如果我在同一个vb中有两个或多个线程.net应用程序,他们每个人都调用共享函数在同一时间与不同的RADIUS,他们每个人都会得到自己的区域?


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


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

每次调用都会创建一个新的实例本地变量。
$ b


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.

解决方案

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.

这篇关于VB.NET共享函数,如果多次同时调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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