<分区>
大家好。我开发了以下代码。目的是:在屏幕上每隔 500 毫秒打印一次当前时间。这应该发生在线程内。我的代码不起作用,我不知道为什么。
====================================================================
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimer implements Runnable
{
public class PrintingTask extends TimerTask
{
public void run()
{
Date CurrentTime = new Date() ;
System.out.println(CurrentTime) ;
}
}
public void run()
{
Timer timer = new Timer() ;
PrintingTask Task1 = new PrintingTask() ;
timer.schedule(Task1,500);
}
}
//====================End of the thread : MyTimer========================
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread TimerOfScreen = new Thread(new MyTimer());
TimerOfScreen.start();
}
======================End of the test class=====================
日期只打印一次,不是每 500 毫秒打印一次。任何机构都可以修复此代码,“逻辑上没有大的变化”?