Often we might have requirement to put a time delay between two statements in PeopleCode. This is often involved when we deal with files. We might need to wait for some time before the file is available and once it is available, we need to continue with the execution.
The frequent method we use in PeopleSoft is to put a while loop and we will loop it for 1000 or 10000 times depending upon the delay required. This will achieve the functionality we require and the delay is introduced. I was under the belief that this is the only and best method to achieve delay in PeopleCode until I read another thread regarding the execution delay code.
Before going to the discuss the alternative method to delay execution of code, I will tell why this is not the best method to do so.
11. With do while we are introducing delay by looping for n times. So we cannot say to delay for a fixed time say 10 seconds. The looping time varies from hardware to hardware and hence we cannot create a generalized formula which says n iterations equal 1 second or so. Every time we need to do a trial and error method to find the correlation between time and iterations.
22. The major drawback of this method is that it is utilizing the application server, thereby reducing the available resource for other users. Since it is a loop, app server will be busy with executing the loop code the entire time delay we have provided. This is a kind of server overloading or a bad practice.
Now I will tell you the method I have seen to introduce the delay without having the major drawbacks mentioned above. The approach is to use sleep method of java class. With this method we can specify the time in seconds/milliseconds for the delay. Also unlike the loop method, the sleep will not cause the application server to execute and consume resources. It will simply stop execution of the thread and app server goes to sleep unless the time limit is reached, then the execution will be resumed. The code to achieve the same will be as below.
/* Assign the number of seconds to sleep */
&SecondToSleep = 2;
/* The below code will delay the execution for 2 seconds */
GetJavaClass("java.lang.Thread").sleep(&SecondToSleep * 1000);
You are welcome to share if there is any other better alternative for the same feature that you might be aware of.