Vit*_*ruz 4 multithreading smalltalk thread-local pharo
在Pharo中是否存在Java的ThreadLocals,或者实现类似行为的方法?例如,在Hibernate中,ThreadLocals用于通过单个getCurrentSession方法调用提供一个线程(当前请求/上下文)"作用域"统一工作实例 - 在Hibernate上命名为Session.开发人员不必担心,只需相信该方法将返回正确的工作单元.Pharo有可能吗?
我在Pharo Books(Pharo示例,Pharo企业和Deep Pharo)以及此页面中浏览了这个,但找不到有用的信息.
在Pharo中,您使用的是子类ProcessLocalVariable.例如:
"Create a class to store you variable"
ProcessLocalVariable subclass: #MyVariable.
"Use it like this"
MyVariable value: myValue.
"Inside your code, access to current value like this"
MyVariable value.
Run Code Online (Sandbox Code Playgroud)
请注意,比线程局部变量更强大,你有"动态变量",它们相对于执行堆栈(比线程更精确)你可以像这样使用它:
"Create a class to store you variable"
DynamicVariable subclass: #MyVariable.
"Use it like this"
MyVariable
value: myValue
during: [
"... execute your code here... usually a message send"
self doMyCode ].
"Inside your code, access to current value like this"
MyVariable value.
Run Code Online (Sandbox Code Playgroud)
这种变量提供相同的功能(它们甚至更强大),通常是最好的替代品.
| 归档时间: |
|
| 查看次数: |
567 次 |
| 最近记录: |