java 定时器的使用

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Timer;
import java.util.TimerTask;

public class ProgramB {

    public static void main(String[] args) throws IOException {
        Timer timer = new Timer();
        //里面的run方法名是固定写法
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("定时任务被执行了");
            }
        };

        timer.schedule(task,0, 1000);
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        reader.readLine();
    }

}

 

posted on 2023-01-12 17:15  空明流光  阅读(35)  评论(0编辑  收藏  举报

导航