JAVA中 如何使用延迟?

Python020

JAVA中 如何使用延迟?,第1张

Java中主要有两种方法来实现延迟,即:Thread和Timer

1、普通延时用Thread.sleep(int)方法,这很简单。它将当前线程挂起指定的毫秒数。如

try

{

Thread.currentThread().sleep(1000)//毫秒

}

catch(Exception e){}

在这里需要解释一下线程沉睡的时间。sleep()方法并不能够让程序"严格"的沉睡指定的时间。例如当使用5000作为sleep()方法的参数时,线 程可能在实际被挂起5000.001毫秒后才会继续运行。当然,对于一般的应用程序来说,sleep()方法对时间控制的精度足够了。

2、但是如果要使用精确延时,最好使用Timer类:

Timer timer=new Timer()//实例化Timer类

timer.schedule(new TimerTask(){

public void run(){

System.out.println("退出")

this.cancel()}},500)//五百毫秒

这种延时比sleep精确。上述延时方法只运行一次,如果需要运行多次, 使用timer.schedule(new MyTask(), 1000, 2000)则每间隔2秒执行MyTask()

配置web.xml

<listener>

<listener-class>com.yohn.timer.ExecTimer</listener>

</listener>

********************************************************************

java源文件

package com.yohn.timmer

import java.io.BufferedWriter

import java.io.File

import java.io.FileWriter

import java.io.IOException

import java.util.Date

import java.util.Timer

import java.util.TimerTask

import javax.servlet.ServletContextEvent

import javax.servlet.ServletContextListener

public class SetTimerExecute extends TimerTask implements ServletContextListener

{

public void contextDestroyed(ServletContextEvent arg0)

{

System.out.println("服务器停止")

}

public void contextInitialized(ServletContextEvent arg0)

{

System.out.println("服务器启动")

//新建一个时间控件

Timer t = new Timer()

//指定的任务从指定的延迟后开始进行重复的固定延迟执行。

t.schedule(new SetTimerExecute(),1000,1000)

}

public void run()

{

FileWriter fs = null

BufferedWriter buf = null

try

{

File f = new File("/test.txt")

if (!f.exists())

f.createNewFile()

fs = new FileWriter(f,true)

buf = new BufferedWriter(fs)

//每天上午7点写入到文件

if(new Date().getHours()==7)

{

SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd")

System.out.print("起床啦!起床啦!")

f.write("定时打印:" + myFormatter.format(new Date()))

buf.newLine()

}

}

catch (Exception e)

{

e.printStackTrace()

}

finally

{

try

{

buf.flush()

fs.close()

}

catch (IOException e)

{

}

}

}

}

setTimeout()在js类中的使用方法

setTimeout (表达式,延时时间)

setTimeout(表达式,交互时间)

延时时间/交互时间是以豪秒为单位的(1000ms=1s)

setTimeout 在执行时,是在载入后延迟指定时间后,去执行一次表达式,仅执行一次

setTimeout 在执行时,它从载入后,每隔指定的时间就执行一次表达式

1,基本用法:

执行一段代码:

var i=0

setTimeout("i+=1alert(i)",1000)

执行一个函数:

var i=0

setTimeout(function(){i+=1alert(i)},1000)

//注意比较上面的两种方法的不同。

下面再来一个执行函数的:

var i=0

function test(){

i+=1

alert(i)

}

setTimeout("test()",1000)

也可以这样:

setTimeout(test,1000)

总结:

setTimeout的原型是这样的:

iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])

setTimeout有两种形式

setTimeout(code,interval)

setTimeout(func,interval,args)

其中code是一个字符串

func是一个函数.

注意"函数"的意义,是一个表达式,而不是一个语句.

比如你想周期性执行一个函数

function a(){

//...

}

可写为

setTimeout("a()",1000)

setTimeout(a,1000)

这里注意第二种形式中,是a,不要写成a(),切记!!!

展开来说,不管你这里写的是什么,如果是一个变量,一定是一个指向某函数的变量如果是个函数,那它的返回值就 要是个函数

2,用setTimeout实现setInterval的功能

思路很简单,就是在一个函数中调用不停执行自己,有点像递归

var i=0

function xilou(){

i+=1

if(i>10){alert(i)return}

setTimeout("xilou()",1000)

//用这个也可以

//setTimeout(xilou,1000)

}