gpt4 book ai didi

java - 使用Thread查找2个键的最小距离

转载 作者:行者123 更新时间:2023-12-03 13:08:45 25 4
gpt4 key购买 nike

我从MainKey中的列表中有String键,我正在尝试使用线程查找最小距离。
因此,对于每个键,我必须遍历完整的Set并记录距离。
我是否正确使用了线程,因为在日志中我看到两个线程正在使用相同的键。

  public void matchKeys() {
// read all MAin keys from redis

Set<String> allKeys = redisTemplate.keys("*");
// get the failure keys list
final List<String> FailureList = new ArrayList<>();
FailureList.add("samsung5210e");
FailureList.add("lgSL50");

// thread to find the minimum distacne of all failure keys from the MainKeys list
Thread t = new Thread() {
@Override
public void run() {
int Distance;

for (String e : FailureList) {
for (String s : allKeys) {
Distance = damerauLevenshteinDistancee(e), s);

LOG.log(Level.INFO, "Distance is {0} for Key {1} and the failure Key is {2}", new Object[]{Distance, s, e});


}
}

}
};
t.start();

}

最佳答案

如果我的理解是正确的,则距离计算成本很高,并且您尝试对其进行多线程处理,那么您应该执行以下操作:

for (String e : FailureList) {
for (String s : allKeys) {
Thread t = new Thread() {
@Override
public void run() {
int distance = damerauLevenshteinDistancee(e), s);
LOG.log(Level.INFO, "Distance is {0} for Key {1} and the failure Key is {2}", new Object[]{Distance, s, e});
}
}
t.start();
}
}

关于java - 使用Thread查找2个键的最小距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46462996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com