java 间隔5秒循环执行一个方法

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/sinat_28505133/article/details/79642923
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

	public static void main(String[] args) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Boolean result = false;
		int count = 0;
		while(!result) {
			try {
				Thread.sleep(5 * 1000); //设置暂停的时间 5 秒
				count ++ ;
				System.out.println(sdf.format(new Date()) + "--循环执行第" + count + "次");
				if (count == 3) {
					result = true;
					break ;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}  
		}
	}

}

猜你喜欢

转载自blog.csdn.net/sinat_28505133/article/details/79642923