java 計時器/定時器


計時器/定時器---線程應用

java.util包

Timer類

無參數構造方法 創建對象

timer.schedule();

package test06052;
import java.util.*;
import java.text.SimpleDateFormat;
public class TestTimer{
	private int count=0;
	private ArrayList<String> userBox=new ArrayList<String>();
	{
		userBox.add("a");
		userBox.add("b");
		userBox.add("c");
		userBox.add("d");
	}
	
	public void test() throws Exception{
		Timer time=new Timer();//記錄
		SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date firstTime=s.parse("2020-06-05 22:36:00");
		time.schedule(new TimerTask(){
			public void run(){
				for(int i=0;i<userBox.size();i++){
					System.out.println("給"+userBox.get(i)+"發送消息:垃圾;第"+(++count)+"條");
				}
				System.out.println("做壞事了");
			}
		},firstTime,3000);
	}
	
	public static void main(String[] args){
		try{
			TestTimer tt=new TestTimer();
			tt.test();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM