Results 1 to 5 of 5
  1. #1
    rhm54 is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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.

  3. #3
    rhm54 is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    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.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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);
        }
    }

  5. #5
    rhm54 is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    Alright this is what I did but it doesn't appear to work. Can anyone see any errors in the code?

    Java 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;
                    }
                }
                   
    
    }

Similar Threads

  1. Unablt to call a sessionbean's business method in EJB 3.0
    By amitid4forum in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 11-24-2007, 12:03 PM
  2. Unablt to call a sessionbean's business method in EJB 3.0
    By amitid4forum in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 11-24-2007, 12:00 PM
  3. Replies: 1
    Last Post: 08-07-2007, 06:16 AM
  4. Replies: 1
    Last Post: 04-26-2007, 03:52 PM
  5. How To Call EJB From Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •