【发布时间】:2013-07-25 21:11:04
【问题描述】:
祝大家今天好。
我有一个线程,如下所示,它在其为真条件下,不断检查 HashSet 中的数据,如果它存在,它会提取这些数据并执行某些操作,并且在 HashSet 中 5 分钟内没有符号(这是我的问题我怎样才能在下面的其他块中保持这样的条件是可能的)
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();
}
}
}
【问题讨论】:
标签: java multithreading