java - Is Time Check possible inside while true Thread condition -
good day of .
i have thread shown below , in while true condition , continuously checks data inside hashset , if present extracts , , incase there no symbols 5 minutes in hashset (here question how can keep such condition in below else block possible )
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 , set.clear(); } else { // if there no elements in set more 5 minutes execute task // here question , keep such check 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(); } } }
you can initialize timestamp before loop. then, if set.size() > 0
true, update timestamp current time. in else check whether saved timestamp @ least 5 minutes older current timestamp.
you want this:
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 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(); } } }
Comments
Post a Comment