一、线程实现,代码如下:
import java.util.Date;
class Clockxian extends Thread{
public void run(){
while(true){
System.out.println(new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Clock2 {
public static void main(String[] args){
Clockxian clock = new Clockxian();
clock.start();
System.out.println("秒表启动");
}
}
二、接口实现,代码如下:
import java.util.Date;
class Clock4 implements Runnable{
public void run(){
while(true){
System.out.println(new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Clock3 {
public static void main(String[] args){
Clock4 clock = new Clock4();
Thread thread=new Thread(clock);
thread.start();
System.out.println("秒表启动");
}
}