943,912 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 16864
  • Java RSS
Aug 30th, 2008
0

how to use java.util.Timer;

Expand Post »
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
Similar Threads
Reputation Points: 7
Solved Threads: 0
Light Poster
rapperhuj is offline Offline
40 posts
since Apr 2007
Aug 30th, 2008
0

Re: how to use java.util.Timer;

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nsisupalan is offline Offline
5 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: hibernate.hbm.xml
Next Thread in Java Forum Timeline: how to insert password in cookies??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC