JAVA¶¨Ê±Æ÷ Timer+TimerTask
±¾ÎÄÊ×ÏÈÉè¼ÆÒ»¸öµ¥´Î¶¨Ê±Æ÷Timer+TimerTask£¬È»ºóÔٴλù´¡ÉÏÉè¼ÆÒ»¸öÑ»·¶¨Ê±Æ÷¡£
ÎÒ½«°Ñ java.util.Timer ºÍ java.util.TimerTask ͳ³ÆΪ Java ¼ÆʱÆ÷¿ò¼Ü£¬ËüÃÇʹ³ÌÐòÔ±¿ÉÒÔºÜÈÝÒ׵ؼƻ®¼òµ¥µÄÈÎÎñ£¨×¢ÒâÕâЩÀàÒ²¿ÉÓÃÓÚ J2ME ÖУ©¡£²»¹ý£¬Java ¼ÆʱÆ÷¿ò¼ÜûÓÐ×ã¹»µÄÄÜÁ¦À´Âú×ãÐí¶àÓ¦ÓóÌÐòµÄ¼Æ»®ÒªÇó¡£ÉõÖÁÒ»ÏîÐèÒªÔÚÿÌìͬһʱ¼äÖظ´Ö´ÐеÄÈÎÎñ£¬Ò²²»ÄÜÖ±½ÓʹÓà Timer À´¼Æ»®£¬Òò´ËÎÒÃǽ«ÔÙTimer+TimerTaskµÄ»ù´¡ÉÏÉè¼ÆÒ»¸öͨÓõÄÑ»·¶¨Ê±Æ÷£¬ÕâÀï³Æ֮Ϊ¼Æ»®¿ò¼Ü£¬´Ó¶øÔÊÐí¸üÁé»îµÄ¼Æ»®ÈÎÎñ¡£Õâ¸ö¿ò¼Ü·Ç³£¼òµ¥ ¨D¨D Ëü°üÀ¨Á½¸öÀàºÍÒ»¸ö½Ó¿Ú ¨D¨D ²¢ÇÒÈÝÒ×ÕÆÎÕ¡£
Ò»¡¢¼Æ»®µ¥´ÎÈÎÎñ
ÏëÏñÒ»¸öÖóµ°¼ÆʱÆ÷£¬ÔÚÊý·ÖÖÓÖ®ºó£¨Õâʱµ°ÖóºÃÁË£©Ëü»á·¢³öÉùÒôÌáÐÑÄú¡£Listing 1 ¹¹³ÉÁËÒ»¸ö¼òµ¥µÄÖóµ°¼ÆʱÆ÷µÄ»ù±¾½á¹¹£º
¡¾Listing 1£ºµ¥´Î¶¨Ê±Æ÷Timer+TimerTask¡¿
import java.util.Timer;
import java.util.TimerTask;
/********************************************************************
* µ¥´Î¼ÆʱÆ÷ Timer+TimerTask×éºÏ
* TimerÀàÊÇḬ̈߳²È«µÄ£º¶à¸öÏ߳̿ÉÒÔ¹²Ïíµ¥¸ö Timer ¶ÔÏó¶øÎÞÐè½øÐÐÍⲿͬ²½¡£
* Óëÿ¸öTimer¶ÔÏóÏà¶ÔÓ¦µÄÊǵ¥¸öºǫ́Ị̈߳¬ÓÃÓÚ˳ÐòµØÖ´ÐÐËùÓмÆʱÆ÷ÈÎÎñ¡£
*/
public class EggTimer {
private final Timer timer = new Timer();
private final int minutes;
public EggTimer(int minutes) {
this.minutes = minutes;
}
public void start() {
timer.schedule(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}
private void playSound() {
System.out.println("Your egg is ready!");
}
}, minutes * 1000);
}
public static void main(String[] args) {
EggTimer eggTimer = new EggTimer(2);
eggTimer.start();
}
}
EggTimer ÀàÓµÓÐÒ»¸ö Timer ʵÀý£¬ÓÃÓÚÌṩ±ØÒªµÄ¼Æ»®¡£Óà start() ·½·¨Æô¶¯Öóµ°¼ÆʱÆ÷ºó£¬Ëü¾Í¼Æ»®ÁËÒ»¸ö TimerTask £¬ÔÚÖ¸¶¨µÄ·ÖÖÓÊýÖ®ºóÖ´ÐС£Ê±¼äµ½ÁË£¬ Timer ¾ÍÔÚºǫ́µ÷Óà TimerTask µÄ start() ·½·¨£¬Õâ»áʹËü·¢³öÉùÒô¡£ÔÚÈ¡Ïû¼ÆʱÆ÷ºóÕâ¸öÓ¦ÓóÌÐò¾Í»áÖÐÖ¹¡£
¶þ¡¢¼Æ»®Öظ´Ö´ÐеÄÈÎÎñ
ͨ¹ýÖ¸¶¨Ò»¸ö¹Ì¶¨µÄÖ´ÐÐƵÂÊ»òÕ߹̶¨µÄÖ´ÐÐʱ¼ä¼ä¸ô£¬ Timer
Ïà¹ØÎĵµ£º
Á½ÖÖ½¨Á¢Ï̶߳ÔÏóµÄ·½·¨£º
Extends Thread
implement Runnable
ÕâÁ½ÖÖ·½·¨¶¼¿ÉÒÔ´´½¨Ò»¸öÏ̶߳ÔÏó£¬
public class TestThread {
public static void main(String arg[]){
newThread thread = new ne ......
Èç¹ûÒªÔËÐÐjava³ÌÐò¾Í±ØÐë°²×°JRE£¨Java Runtime Environment)
Èç¹ûÒª¿ª·¢java³ÌÐò¾Í±ØÐë°²×°JDK£¨Java Develope Kit)
£¬×¢ÒâJDKÖаüÀ¨ÁËJRE¡£
°²×°Íê³Éºó£¬ÐèÒªÐÞ¸Äϵͳ»·¾³±äÁ¿PATH·½·¨£¬²ÅÄÜʹϵͳ֪µÀµ½ÄÄÀïÈ¥ÕÒjavacµÈÏà¹Ø±àÒëÐèÒªµÄÃüÁ·½·¨ÈçÏ£º
1¡¢ÓÒ¼üµ¥»÷ÎҵĵçÄÔ£¬Ñ¡‘ÊôÐÔ’²Ëµ¥£¬ÔÚµ¯³öµÄ¶Ô» ......
1. ±äÁ¿²»×ö³õʼ»¯¡£
2. ·ÖÅäµÄ¶ÔÏó²»×öÏú»Ù¡£Ò»¸öºÃµÄÏ°¹ß¾ÍÊÇ£¬Ã¿Éú³ÉÒ»¸öÀàʱ£¬¾Í×Ô¶¯Ð´Ò»¸ö×Ô¼ºµÄInitInstanceMember,ºÍ ReleaseInstanceMember()·½·¨¡£
3. °üµÄ·ÖÀ࣬Èç¹û×Ô¼º²»×ösourceµÄÂß¼·ÖÀ࣬C++»·¾³Ò»°ãÊDz»»á×Ô¼º×öµÄ¡£Õâ¸ö¸úJavaÖеÄpackageºÍÎļþÎïÀíÉÏ´æ´¢·½Ê½Ò»ÖÂÊDz»Ò»ÑùµÄ¡£
C++ ......
JAVA±ÊÊÔÌ⾫»ª°æ1.0
public class Test2
{
public static void main(String[] args)
{
Float a = new Float(3.4);
&n ......