0

hi its me again.. curious thingking about how to control a timer statements per clock tick processes with if else statement

i would like to perform this operations like;
clock.start();
if(clocktime == 1)
System.out.println("Hi");
else if(clocktime == 2)
System.out.println("Hellow");
clock.stop();

i know this program is an error because of incompatible types so can you give me some suggestions and comments about how to perform or code it in a right way..

can u give me some codes regarding to the question

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by nsisupalan
0

Try somthing like this:

package com.mine.test;

import java.util.Calendar;
import java.util.Timer;

public class Initiater {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Timer clock = new Timer();
		for(int i=0;i<2;++i)
			clock.scheduleAtFixedRate(new MyTimerTask(clock,i),Calendar.getInstance().getTime(),500);
	}

}
package com.mine.test;

import java.util.Timer;
import java.util.TimerTask;

public class MyTimerTask extends TimerTask {
	
	private int counter = 0;
	private Timer timer = null;
	
	public MyTimerTask(Timer timer,int counter) {
		this.timer = timer;
		this.counter = counter;
	}

	@Override
	public void run() {
		switch(counter) {
		case 0:
			System.out.println("Hello");
			break;
		case 1:
			System.out.println("Hellow");
			this.timer.cancel();
		}
	}
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.