*
The moose likes Java in General and the fly likes How to set a time limit on a java function running a regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to set a time limit on a java function running a regex" Watch "How to set a time limit on a java function running a regex" New topic
Author

How to set a time limit on a java function running a regex

Yatendra Goyal
Greenhorn

Joined: Aug 14, 2009
Posts: 5
I am running a regex in a java function to parse a document and return true if it has found the string specified by the regex and return false if it hasn't. But the problem is that when the document doesn't contain the string specified by the regex It takes a very long time to return false and I want to terminate that function if it takes more than 6 seconds to execute.

How can I set a time limit of 6 seconds on that function so as to forcibly terminate that if it takes more than 6 seconds.

Thanks in advance.


Yatendra Goel
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Unless you have a huge file that you fire up your regexp on, it should not take that long.

There is a little workaround to specify a time limit, by letting the regexp execute in a separate thread.

Check this code:



That should be it...


JDBCSupport - An easy to use, light-weight JDBC framework -
Yatendra Goyal
Greenhorn

Joined: Aug 14, 2009
Posts: 5
First of all a very thanks for your answer. But I have one more problem. I want to return a value from the method but I think this is not possible between two threads so can you suggest me other alternative to set a limit on the execution of the method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18534
    
  40

Yatendra Goyal wrote:First of all a very thanks for your answer. But I have one more problem. I want to return a value from the method but I think this is not possible between two threads so can you suggest me other alternative to set a limit on the execution of the method.


Why isn't it possible to return a value? In fact, the example provided does return a value. Admittedly, it is a boolean value, but can't you change it to return something else instead?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Yep,

note that after the 6 seconds passed and the function will just return, the Thread will still keep running and do it's job until it's done.
Yatendra Goyal
Greenhorn

Joined: Aug 14, 2009
Posts: 5
Sebastian Janisch wrote:Yep,

note that after the 6 seconds passed and the function will just return, the Thread will still keep running and do it's job until it's done.



thanks Janisch Yep, but I am confused but it is urgent for me to complete this job very soon. Can we communicate right now so that I can tell you my problem exactly what I need to do.... I really need your help... my email-id is <removed>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

http://faq.javaranch.com/java/UseTheForumNotEmail. I've removed your email address (added bonus: spammers won't find it).

I think Sebastian has already shown you how to do it. Granted, the regex code will still run in the background, but the result will be in after around 6 seconds.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yatendra Goyal
Greenhorn

Joined: Aug 14, 2009
Posts: 5
Rob Prime wrote:http://faq.javaranch.com/java/UseTheForumNotEmail. I've removed your email address (added bonus: spammers won't find it).

I think Sebastian has already shown you how to do it. Granted, the regex code will still run in the background, but the result will be in after around 6 seconds.


I tell you my problem in detail:

I am calling a method "method 1" of class 2 from class 1. The "method 1" calls "method 2" of the same class i.e. "class 2". Method 2 runs the regex code over a document. If it finds the string specified by the regex, then it returns the result to the method 1 which in turn return the result to the method in "class 1" which called the "method 1" of class 2.
Now the problem is that the execution time of both the method1 and method2 of class 2 should be not more than 6 seconds.

So, I made a new RegexpThread class in the same file, in which my class2 was. Then I move the method2 of the class2 into class RegexpThread. Then whenever the method 1 is called, it instantiates the RegexpThread class as follows:



The code shown is in the method 1 of class2. The method 2 in the RegexpThread class perform some regex search over a document. There is a private field named "resultXml" in the RegexpThread class. If the method 2 has found the string specified by the regex then it assigns the result to the private field "resultXml". If not then the "resultXml" contains its default value i.e empty string.

So, in the above "if block", it is checking the "resultXml" field against empty string. If it is an empty string then that means the regex has not found its string in the document. But if it is not an empty string then that means the regex has found the string in the document and has assigned the result to the "resultXml" field.

so, look at this and tell me what to do...


[HENRY: added code tags]
Yatendra Goyal
Greenhorn

Joined: Aug 14, 2009
Posts: 5
Yatendra Goyal wrote:
Rob Prime wrote:http://faq.javaranch.com/java/UseTheForumNotEmail. I've removed your email address (added bonus: spammers won't find it).

I think Sebastian has already shown you how to do it. Granted, the regex code will still run in the background, but the result will be in after around 6 seconds.


I tell you my problem in detail:

I am calling a method "method 1" of class 2 from class 1. The "method 1" calls "method 2" of the same class i.e. "class 2". Method 2 runs the regex code over a document. If it finds the string specified by the regex, then it returns the result to the method 1 which in turn return the result to the method in "class 1" which called the "method 1" of class 2.
Now the problem is that the execution time of both the method1 and method2 of class 2 should be not more than 6 seconds.

So, I made a new RegexpThread class in the same file, in which my class2 was. Then I move the method2 of the class2 into class RegexpThread. Then whenever the method 1 is called, it instantiates the RegexpThread class as follows:

RegexpThread rt = new RegexpThread() {
public void run() {
method 2(m, urlCopy, document);
}

};

rt.start();


rt.start();

try {
rt.join(6 * 1000);
} catch (InterruptedException e) {
return "y";
}

if(rt.getResultXml().equals("")) {
return "g";
}

resultXml.append(rt.getResultXml());
}
return resultXml.toString();


The code shown is in the method 1 of class2. The method 2 in the RegexpThread class perform some regex search over a document. There is a private field named "resultXml" in the RegexpThread class. If the method 2 has found the string specified by the regex then it assigns the result to the private field "resultXml". If not then the "resultXml" contains its default value i.e empty string.

So, in the above "if block", it is checking the "resultXml" field against empty string. If it is an empty string then that means the regex has not found its string in the document. But if it is not an empty string then that means the regex has found the string in the document and has assigned the result to the "resultXml" field.

so, look at this and tell me what to do...




Have you seen my code and my problem. I need a solution and thus need your help if you wish. Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

http://faq.javaranch.com/java/PatienceIsAVirtue
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18534
    
  40

Yatendra Goyal wrote:
Have you seen my code and my problem. I need a solution and thus need your help if you wish. Thanks in advance.


Take a look at the solution that Sebastian gave you. It doesn't look like you are following the example. In fact, it doesn't even look like your code will even compile.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to set a time limit on a java function running a regex
 
Similar Threads
HttpURLConnection.connect() takes long time (3 minutes)
Please help me this. Im new to this.
P6spy does not create spy.log for stand-alone Java program
getting a clock to update correctly
ScheduledExecutorService - Time To Live