java每隔一秒读取文件_想用timer实现每隔1秒执行一次abc()函数。不知道该如何写?...

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 00:04   11   0

请给代码

|

import java.util.*;

public class test{

public static void main(String[] args){

MyTask task = new MyTask();

java.util.Timer timer = new Timer();

timer.schedule(task, 1000, 10000);

}

}

class MyTask extends java.util.TimerTask{

void abc(){

System.out.println("abc()");

}

public void run(){

try{

while(true){

abc();

Thread.sleep(3000);

}

}catch(java.lang.InterruptedException ie){

}

}

}

|

public class myThread extends Thread {

public int num;

int threadid;

public myThread(int id) {

num=0;

threadid=id;

}

public void run() {

// compute primes larger than minPrime

for(;;)

{

num++;

try{

sleep(500);

}catch(Exception e)

{

}

System.out.println("mythread go threadid="+Integer.toString(threadid)+" num="+Integer.toString(num));

}

}

}

//thread

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP