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){} }} }