• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Yatendra Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Yatendra Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yatendra Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/PatienceIsAVirtue
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic