This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Looking for a logic to insert delay in our java code  RSS feed

 
Muthu Ponmozhi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
We have a MDB having some external API cal in a loop.We need to insert a delay in between each such API call. We already have a logic with Creating Date objects in an infinite loop causing outofmemory error at runtime. so need a nice logic which will just insert a delay of 1 second in my java code.
Pls note that Thread concepts are not allowed here because this code part resides in an EJB.

Thanks 'n advance.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls note that Thread concepts are not allowed here because this code part resides in an EJB.


What is wrong with calling the Thread.sleep() method? You are not starting any new threads. All it does is cause the current thread, which is running your EJB method, to sleep (wait) for a bit.

Henry
[ February 09, 2007: Message edited by: Henry Wong ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree - sleep oughtta be fine. You'll just simulate a slightly longer running task as far as the container is concerned.

Re threads in EJBs - while creating your own is forbidden by the EJB spec, some container vendors have a supported way around it, ie a pacakge that will manage threads for you. If you can tolerate non-portable code, see what your container docs say or let us know what "brand" you're running and see if anybody pops up with an answer.
 
Muthu Ponmozhi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks a lot for your replies. Actually i have no idea on why do we restrict thread in our code. Anyways, we found a way to insert the delay. here is the code we had earlier which caused performance issue and new code which we tuned for this.

public void pause( long numberMillis )
{
/*Commmented for tuning memory usage
Date now = new Date();
long exitTime = now.getTime() + numberMillis;
while( true )
{
now = new Date();
if( now.getTime() > exitTime )
return;
}
*/
//New code added for the same 2/9/2007
int count =0;
long exittime = System.currentTimeMillis() + numberMillis;
System.out.println("exittime"+exittime);
while(true)
{
count++;
System.out.println("count of iterations--"+count);
if(System.currentTimeMillis() > exittime)
{
System.out.println("count of iterations"+count);
return;
}
}
}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Henry and Stan tried to say was that even if you are restricted from using threads, you should still be able to use Thread.sleep().

Thread.sleep() isn't "using threads" any more than Java is using threads, anyway - you literally can't write a Java program without using at least one thread (the main thread the JVM creates to run the main method).

To reiterate again, a call to Thread.sleep() should be totally safe for you, and your workaround is just, with all due respect, insane.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!