Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Timer expires invoke method

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Timer expires invoke method

    Hi ,
    i want to set a timer for 5 secs and once the timer wakes up i want to invoke a method . i am checking a boolean flag of class b,once set i want to invoke the method . but the logic is not quite working ,any other simple method to do this thing??

    i have implemented this using a thread...
     
    public class a {
     
    	int seconds; 
    	Thread timer;
    	static b obj=new b();
     	public static void main(String[] args) {
     
     
     
     		Thread t1=new Thread(obj);
    t1.start();
     
    while(true){
     
    //	System.out.println(b.flag);            //..................>if i use this line the code works fine ??
    	if(b.flag)
    	{System.out.println("timer was set");
    	System.out.println("this should be true"+b.flag);
    		b.flag=false;
    		System.out.println("this should be false"+b.flag);
    	System.out.println("method");
    	System.out.println("timer cleared");
     
    	}
     
     
     
    }
     
     	}
    }
     
     
    public class b implements Runnable{
    	int seconds;
    	public static boolean flag=false;
     
     
    	public void run() {
    		while(true)
    			{
    			System.out.println("flag being set");
     
    			flag=true;
    			 	//	try{
    			 			try {
    							Thread.sleep(5000);
    						} catch (InterruptedException e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						}
    			 		//}catch(Exception e){}
     
     	}}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Timer expires invoke method

    Have you looked at using the Swing Timer class. It could be easier.

    but the logic is not quite working
    Can you explain what happens or doesn't happen?

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Timer expires invoke method

    the logic

    if(b.flag)
    {System.out.println("timer was set");
    System.out.println("this should be true"+b.flag);
    b.flag=false;
    System.out.println("this should be false"+b.flag);
    System.out.println("method");
    System.out.println("timer cleared");

    }

    is not performed even the flag is being set

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Timer expires invoke method

    is not performed even the flag is being set
    What is the value of b.flag that is printed out when the println is uncommented?

    What was printed out when you ran it?

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Timer expires invoke method

    the output is like:


    flag being set
    flag being set
    flag being set


    this body:

    if(b.flag)
    {System.out.println("timer was set");
    System.out.println("this should be true"+b.flag);
    b.flag=false;
    System.out.println("this should be false"+b.flag);
    System.out.println("method");
    System.out.println("timer cleared");

    }


    is not executing
    Last edited by jack_nutt; July 11th, 2011 at 05:32 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Timer expires invoke method

    Here's what I get when I execute it:


    Running: D:\Java\jdk1.6.0_25\jre\bin\java.exe -Xmx512M -classpath D:\JavaDevelopment;. a

    flag being set
    timer was set
    this should be truetrue
    this should be falsefalse
    method
    timer cleared
    flag being set
    timer was set
    this should be truetrue
    this should be falsefalse
    method
    timer cleared
    flag being set
    timer was set
    this should be truetrue
    this should be falsefalse
    method
    timer cleared
    flag being set
    timer was set
    this should be truetrue
    this should be falsefalse
    method
    timer cleared

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Timer expires invoke method

    I have tried this on Eclipse and Netbeans IDE .
    I got this :


    flag being set
    timer was set
    this should be truetrue
    this should be falsefalse
    method
    timer cleared
    flag being set
    flag being set
    flag being set
    flag being set
    flag being set

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Timer expires invoke method

    Sorry, it seems to work for me.

Similar Threads

  1. how to set timer
    By jack_nutt in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 08:55 PM
  2. Updating timer
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 26th, 2011, 07:02 AM
  3. how to invoke a method?????
    By amr in forum Java Theory & Questions
    Replies: 2
    Last Post: December 2nd, 2010, 11:48 PM
  4. Ticked Off Timer
    By JavaCow in forum Loops & Control Statements
    Replies: 8
    Last Post: October 30th, 2010, 09:18 AM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM