how to use java.util.Timer;

Reply

Join Date: Apr 2007
Posts: 40
Reputation: rapperhuj is an unknown quantity at this point 
Solved Threads: 0
rapperhuj rapperhuj is offline Offline
Light Poster

how to use java.util.Timer;

 
0
  #1
Aug 30th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 5
Reputation: nsisupalan is an unknown quantity at this point 
Solved Threads: 0
nsisupalan nsisupalan is offline Offline
Newbie Poster

Re: how to use java.util.Timer;

 
0
  #2
Aug 30th, 2008
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();
		}
	}
}
Last edited by nsisupalan; Aug 30th, 2008 at 11:02 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC