0

大家好。

我有一个线程,如下所示,它在它的真实条件下,不断检查 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();
        }
    }

}
4

3 回答 3

1

您可以在循环之前初始化时间戳。然后,如果set.size() > 0为真,则将时间戳更新为当前时间。在 else 中,您检查保存的时间戳是否比当前时间戳至少早 5 分钟。

你可能想要这样的东西:

package com;

import java.util.HashSet;
import java.util.Date;

public class Tester extends Thread
{
    HashSet<String> set = new HashSet<String>();

    public void run() {
        Date d = new Date();
        while (true) {
            try {
                if (set.size() > 0) {
                    d = new Date();
                    set.clear();
                }
                else {
                    if(new Date().getTime() - d.getTime() > 300000){
                        d = new Date();
                        //execute your method
                    }
                }

                Thread.sleep(2000);
            } catch (Exception e) {
            }
        }
    }

    public static void main(String args[]) {
        try {
            Tester qT = new Tester();
            qT.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
于 2013-07-25T19:34:38.890 回答
0

首先,创建一个计时器:

Timer timer = new Timer(300000, new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        <preform the task here>
    }
});
timer.setRepeats(false);

当线程启动时,启动计时器:

timer.start();

如果集合中有项目:

timer.restart();

不需要 else,计时器会处理这个问题。您应该检查timer.isRunning主循环条件,以便对设置元素的检查在 5 分钟后停止。

于 2013-07-25T19:37:21.377 回答
0

当线程进入运行时,获取 SystemTime。还可以在 else 块中获取当前时间,如下所示: 另外,如果我们从 hashset 中获取数据,只需计算新的系统时间 t1 包 com;

import java.util.HashSet;

 public class Tester extends Thread

{

HashSet<String> set = new HashSet<String>();

public void run() {
    long t1 = date.getTime();
    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 

 long t1 = date.getTime();
 if(!hashset_data_not_available)
 {
 t1 = date.getTime();
 }
 if((t2-t1)/(60*1000)>5 && if_hashset_data_not_available) {
//do something that u wanna do 
{
            }

            Thread.sleep(2000);
        } catch (Exception e) {
于 2013-07-25T19:42:12.307 回答