Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread for periodic database updation  RSS feed

 
Nidhi Singhal
Ranch Hand
Posts: 89
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want make a thread that once started would periodically keep updating the database (after a specifed interval). I have the database logic in place.

My code is something like this:

t.start();

while(true)
{
System.out.println("Inside while(true) Loop");

if(t.isAlive() == true)
{
System.out.println("Inside t.isAlive() == true");
<update database>
}

try {
synchronized(t)
{
t.wait(3000);
}
} catch (InterruptedException e){
System.out.println("Thread interrupted" + e.toString());
}
}

The above code does not update the database but, when I comment out
while(true) loop, it updates the database. But I want the database to be updated periodically say after every 3000s.
How should I go about it?
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we need to see all the code, I'm worried at to how much of that code sits in your run method or not.

Anyway I would have thought a Timer, java.util.Timer was better suited for this job
 
Nidhi Singhal
Ranch Hand
Posts: 89
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there isn't anything inside run(). This is because my database code in a different class and I am not able to call the same from run(). I am using BEA workshop for development and all the db queries lies inside a database control which I am unable to call from run().
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into java.util.Timer and TimerTask. The best place for the "<update database>" stuff is in the run method of a task.

This is because my database code in a different class and I am not able to call the same from run().


I bet we can get you past that. Why can't you call the other class from run?
 
Achalveer Singh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
you can call your another class in run method. simply make object in run method and call method in which you are updating database..
like
Thread t = new Thread();
while(true)
{
t.start();
}
public void run()
{
ClassName obj = new ClassName();
obj.updateDatabase();
try
{
t.sleep(3000);
}catch(Exception e)
{
}
}
you can do in that way easily.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!