「java控制时间」sleep java时间控制
今天给各位分享java控制时间的知识,其中也会对sleep java时间控制进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、JAVA 控制时间
- 2、Java怎么控制运行时间不超过指定的时间上限
- 3、java中如何控制时间间隔?
- 4、java如何控制有效时间在8.00-24.00
- 5、java控制循环内执行时间
- 6、java 如何设定时间执行程序?
JAVA 控制时间
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Test extends TimerTask {
public static void main(String[] args) {
Test thisClass = new Test();
Timer timer = new Timer(true);
timer.schedule(thisClass, 60 * 1000); // 60秒后启动时间任务(Test的run方法)
try {
thisClass.cirStart(); // 开始循环
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private boolean cirFlg = true;
private void cirStart() throws InterruptedException {
while (cirFlg) {
System.out.println(new Date());
Thread.sleep(300);
}
}
@Override
public void run() {
System.out.println("END");
cirFlg = false; // 设定Flg为false,停止循环
}
}
Java怎么控制运行时间不超过指定的时间上限
在main最后加入Thread.sleep(Integer.MAX_VALUE); 让mainThread无限的睡眠。 使用timer来控制程序的结束。 publicclass MainThreadTest { int time =10; //mainThread 存活的时间(s) Timer timer =new Timer(time *1000, new StopListener()).
java中如何控制时间间隔?
我们可以使用Timer和TimerTask类在java中实现定时任务,详细说明如下:
1、基础知识
java.util.Timer
一种线程设施,用于安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。此类是线程安全的:多个线程可以共享单个 Timer 对象而无需进行外部同步。
java.util.TimerTask
由 Timer 安排为一次执行或重复执行的任务。
2、示例代码
该示例实现这样一个功能,在系统运行期间,每30分钟,系统自动检查连接池中的可用连接数,并输出到日志中。
首先创建一个需要定时执行的任务类,这个任务类需要继承TimerTask,然后重写run()方法,run()方法体中的代码就是定时需要执行的操作,在本demo中,就是获取连接池中当前可用连接数,并输出到日志中,具体实现代码如下:
public class TaskAvailableConnectNumber extends TimerTask {
private Logger log = Logger.getLogger(TaskAvailableConnectNumber.class);
private ConnectionPool pool=ConnectionPool.getInstance();
@Override
publicvoid run() {
log.debug("当前连接池中可用连接数"+pool.getAvailableConnectNumber());
}
}
下面定义一个监听器,负责在应用服务器启动时打开定时器,监听器需要实现ServletContextListener接口,并重写其中的contextInitialized()和contextDestroyed()方法,代码如下:
public class OnLineListener implements ServletContextListener{
private Logger log = Logger.getLogger(OnLineListener.class);
Timer timer = null;
//在应用服务器启动时,会执行该方法
publicvoid contextInitialized(ServletContextEvent arg0) {
//创建一个定时器,用于安排需要定时执行的任务。
timer = new Timer();
//为定时器安排需要定时执行的任务,该任务就是前面创建的任务类TaskAvailableConnectNumber,并指定该任务每30分钟执行一次。
timer.schedule(new TaskAvailableConnectNumber(), 0, 30*60*1000);
log.debug("启动定时器");
}
//应用服务器关闭时,会执行该方法,完成关闭定时器的操作。
public void contextDestroyed(ServletContextEvent arg0) {
if(timer!=null){
timer.cancel();//关闭定时器
log.debug("-----定时器销毁--------");
}
}
}
监听器要想正常运行,需要在web.xml文件中进行配置,配置信息如下:
!-- 监听器配置开始 --
listener
listener-class
cn.sdfi.listen.OnLineListener
/listener-class
/listener
!-- 监听器配置结束 --
以上步骤完成后,一个简单的定时器就算开发完成了。
java如何控制有效时间在8.00-24.00
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
public static void main(String[] args) throws ParseException
{
DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
//获得2017年01月14日 的Date对象
Date myDate1 = dateFormat1.parse("2017-1-14");
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获得2017年01月14日14点20分42秒 的Date对象
Date myDate2 = dateFormat2.parse("2017-01-14 14:20:42");
//获得当前时间
Date nowDate = new Date();
//获得当前时间戳 java的Timestamp格式 2017-01-14 14:20:42
//方法1获取结果
Timestamp nowTimestamp = new Timestamp(System.currentTimeMillis());
//方法2
Timestamp nousedate = new Timestamp(nowDate.getTime());
//方法3 Calendar 日历类
Timestamp cTimestamp=Calendar.getInstance().getTimeInMillis();
//只取时--比较SB的方法
DateFormat dateFormat3 = new SimpleDateFormat("HH");
//获得当前几点 string DateFormat.format(当前时间)
//返回的是string 类型
int datehh=(int)dateFormat3.format(nowDate);
if(8=datehh=24)
System.out.println("当前"+datehh+"点");
}
}
java控制循环内执行时间
long t=System.currentTimeMillis();
long t2=System.currentTimeMillis();
while(t2-t5000) {
t2=System.currentTimeMillis();
}
java 如何设定时间执行程序?
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Test {
public static void main(String[] args) {
//timer1();
timer2();
//timer3();
//timer4();
}
// 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)
public static void timer1() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 2000);// 设定指定的时间time,此处为2000毫秒
}
// 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
// schedule(TimerTask task, long delay, long period)
public static void timer2() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 1000, 1000);
}
// 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
// scheduleAtFixedRate(TimerTask task, long delay, long period)
public static void timer3() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 1000, 2000);
}
// 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
// Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
public static void timer4() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时
calendar.set(Calendar.MINUTE, 0); // 控制分
calendar.set(Calendar.SECOND, 0); // 控制秒
Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的12:00:00
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
}
}
java控制时间的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于sleep java时间控制、java控制时间的信息别忘了在本站进行查找喔。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。