请考虑以下代码。每次运行代码时,输出始终为0和3。我确信它与单个实例有关,但没有任何解释为什么会这样。如果您能帮助我理解这一点,那将是很棒的。谢谢您的帮助。
发生这种情况是因为每次初始化变量时,变量都指向堆中的另一个位置吗?
def assert_either_selector(selector_1, selector_2)
if has_selector?(selector_1, wait: false)
assert_selector(selector_1)
elsif has_selector?(selector_2, wait: false)
assert_selector(selector_2)
else
flunk("Failed to match either selector \nExpected to find either: \n\t#{selector_1} or #{selector_2}")
end
end
答案 0 :(得分:2)
这是线程安全的简单情况,而不是线程安全
问题是RunOp
有一个内部Helper
,由于线程睡眠和延迟,该内部public class RunOp
{
private SemaphoreSlim slim = new SemaphoreSlim(1,1);
//Has to do something with single instance
Helper _configHelper;
public async Task Run()
{
await slim.WaitAsync();
_configHelper = new Helper();
var val = await _configHelper.Process();
Console.WriteLine(_configHelper.GetList.Count);
slim.Release();
}
// or
public async Task Run()
{
Helper configHelper = new Helper();
var val = await configHelper.Process();
Console.WriteLine(configHelper.GetList.Count);
}
}
被覆盖并显示不一致的结果。
这是线程安全版本
Data <- structure(list(Date = c("2018-05-25", "2018-05-25", "2018-05-25",
"2018-05-25", "2018-05-25", "2018-05-25", "2018-05-25", "2018-05-25",
"2018-05-25", "2018-05-25", "2018-05-25", "2018-05-25", "2018-05-25",
"2018-05-25", "2018-05-25", "2018-05-25", "2018-05-25", "2018-05-25",
"2018-05-25", "2018-05-25", "2018-05-25", "2018-05-25", "2018-05-25",
"2018-05-25", "2018-05-25"), Time = c("08:36:46.203", "08:36:47.552",
"08:36:48.222", "08:36:49.429", "08:36:50.409", "08:36:51.471",
"08:36:52.393", "08:36:53.422", "08:36:54.482", "08:36:55.436",
"08:36:56.454", "08:36:57.552", "08:36:58.385", "08:36:59.473",
"08:37:00.368", "08:37:01.477", "08:37:02.399", "08:37:03.596",
"08:37:04.457", "08:37:05.593", "08:37:06.595", "08:37:07.582",
"08:37:08.506", "08:37:09.579", "08:37:10.586"), Date_Time = c("2018-05-25 08:36:46.203",
"2018-05-25 08:36:47.552", "2018-05-25 08:36:48.222", "2018-05-25 08:36:49.429",
"2018-05-25 08:36:50.409", "2018-05-25 08:36:51.471", "2018-05-25 08:36:52.393",
"2018-05-25 08:36:53.422", "2018-05-25 08:36:54.482", "2018-05-25 08:36:55.436",
"2018-05-25 08:36:56.454", "2018-05-25 08:36:57.552", "2018-05-25 08:36:58.385",
"2018-05-25 08:36:59.473", "2018-05-25 08:37:00.368", "2018-05-25 08:37:01.477",
"2018-05-25 08:37:02.399", "2018-05-25 08:37:03.596", "2018-05-25 08:37:04.457",
"2018-05-25 08:37:05.593", "2018-05-25 08:37:06.595", "2018-05-25 08:37:07.582",
"2018-05-25 08:37:08.506", "2018-05-25 08:37:09.579", "2018-05-25 08:37:10.586"
)), class = "data.frame", row.names = c(NA, -25L))
我知道这只是一个学术问题,但这确实应该重构并重新考虑