Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-06-2008, 12:56 AM
Member
 
Join Date: Jan 2008
Posts: 12
rhm54 is on a distinguished road
Call a Method Automatically
I need to know how to call a method automatically every 30 seconds. Basically it would work like this:

Every 30 seconds a method would be called that simply popped an object from a queue.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-06-2008, 01:03 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
Make up a background thread in a Runnable. Put a while loop in the run method and sleep for 30 seconds before calling your method.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-06-2008, 01:06 AM
Member
 
Join Date: Jan 2008
Posts: 12
rhm54 is on a distinguished road
Thank you for your help but you are speaking greek to me. I have no clue how to accomplish that. I understand the while statement that will cause it to continually run but the rest I don't understand.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-06-2008, 01:42 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
Code:
public class Test { public static void main(String[] args) { new Test().start(); } private void start() { Thread thread = new Thread(runner); // Useful for/in swing applications: thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } private Runnable runner = new Runnable() { // Or you could have the enclosing class // implement Runnable and put this method // in as an instance method (like this). public void run() { int count = 0; while(true) { try { // Use 3 second delay to prevent boredom. // 30 seconds delay = 30*1000 millis Thread.sleep(3*1000); } catch(InterruptedException e) { System.out.println("interrupt"); break; } method(++count); } } }; private void method(int count) { System.out.println(count); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-07-2008, 09:51 AM
Member
 
Join Date: Jan 2008
Posts: 12
rhm54 is on a distinguished road
Alright this is what I did but it doesn't appear to work. Can anyone see any errors in the code?

Code:
public void removeFromQ() { while(true) { try { Thread.sleep(30*1000); if(listSize() == 0){ System.out.println("Nothing to Print"); } else{ printdata temp = (printdata)getNode(0); System.out.println("Printing " + temp.getPrint() + " for user " + temp.getUser()); if (temp.getUser()== 'A'){ ACount--; } if (temp.getUser()== 'B'){ BCount--; } if (temp.getUser()== 'C'){ CCount--; } removeNode(0); } // Use 3 second delay to prevent boredom. // 30 seconds delay = 30*1000 millis } catch(InterruptedException e) { System.out.println("interrupt"); break; } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unablt to call a sessionbean's business method in EJB 3.0 amitid4forum Enterprise JavaBeans 0 11-24-2007 01:03 PM
Unablt to call a sessionbean's business method in EJB 3.0 amitid4forum Enterprise JavaBeans 0 11-24-2007 01:00 PM
Call a main method from within a running program zoe New To Java 1 08-07-2007 07:16 AM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 04:52 PM
How To Call EJB From Eclipse IDE JavaForums Eclipse 0 04-26-2007 11:15 AM


All times are GMT +3. The time now is 07:29 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org