【问题标题】:Is Time Check possible inside while true Thread condition在真正的线程条件下是否可以进行时间检查
【发布时间】: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


    【解决方案1】:

    您可以在循环之前初始化时间戳。然后,如果set.size() &gt; 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();
            }
        }
    
    }
    

    【讨论】:

    • 非常感谢,如果我有 d = new Date(); ,会不会是对象太多的问题??
    • 应该没问题。新分配后不再存在对旧日期对象的引用。因此垃圾收集器应该清理旧对象。
    • 谢谢,我已经测试了这段代码,因为这个条件总是 true > 300 所以它在 5 分钟后继续执行,我可以避免吗?
    • 抱歉,问题已解决。 getTime() 返回毫秒,而不是秒。忘记乘以 1000。
    • 时间没问题,但我要说的是,一旦满足条件,这将持续执行。
    【解决方案2】:

    首先,创建一个计时器:

    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 分钟后停止对 set 元素的检查。

    【讨论】:

      【解决方案3】:

      当线程进入 run 时,获取 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) {
      

      【讨论】:

      • 你为什么不使用问题中使用的相同条件?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多