在真正的线程条件下,时间检查是否可行

时间:2013-07-25 19:30:53

标签: java multithreading

祝大家度过美好的一天。

我有一个如下所示的Thread,在其条件为true的情况下,持续检查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();
        }
    }

}

3 个答案:

答案 0 :(得分:1)

您可以在循环之前初始化时间戳。然后,如果set.size() > 0为true,则将时间戳更新为当前时间。在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();
        }
    }

}

答案 1 :(得分:1)

当线程进入运行状态时,获取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) {

答案 2 :(得分:0)

首先,创建一个计时器:

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

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

timer.start();

如果集合中有项目:

timer.restart();

没有其他需要,计时器负责。您应该在主循环条件中检查timer.isRunning,以便在5分钟后停止检查设置元素。