VB .NET 共享函数(如果同时调用多次)
考虑我有一个共享函数:-
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
我的问题:
如果我在同一个 vb .net 应用程序中有两个或多个线程,并且每个线程都使用不同的 RADIUS 同时调用共享函数,那么它们每个线程都会获得自己的共享函数吗?区域?
我想知道对函数的每次调用是否使用相同的局部变量或每次调用都会创建局部变量的新实例?
如果我有多个(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:
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?
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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,因为半径值是按值传递的,并且该方法只使用本地声明变量。
每次调用都会创建其局部变量的一个新实例。
是的。同样,因为没有共享的信息存储,并且所有输入都是按值传递,所以它是线程安全的。
Yes, because the radius value is passed by value and the method uses nothing but locally declare variables.
Each call creates a new instance of its local variables.
Yes. Again, because there is no shared storage of information and because all inputs are passed by value, it is thread-safe.
该函数不使用外部状态。它仅访问其局部变量,因此从不同线程调用它是完全安全的。
The function uses no external state. It's only accessing its local variables so it's perfectly safe to call it from different threads.