大家好。
我有一个线程,如下所示,它在它的真实条件下,不断检查 HashSet 中的数据,如果它存在,它会提取这些数据并做一些事情,并且在 HashSet 中 5 分钟内没有符号(这是我的问题我怎么能在 else 块下面保持这样的条件是可能的)
package com;
import java.util.HashSet;
public class Tester extends Thread
{
HashSet<String> set = new HashSet<String>();
public void run() {
while (true) {
try {
if (set.size() > 0) {
// extract those and do something
set.clear();
}
else {
// if there were no elements in set for more than 5 minutes than execute a task
// here is my question , to keep such a check is possible or not
}
Thread.sleep(2000);
} catch (Exception e) {
}
}
}
public static void main(String args[]) {
try {
Tester qT = new Tester();
qT.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}