您的位置:首页 > 编程语言 > Java开发

用线程和接口两种方法实现的java秒表

2015-07-02 23:33 621 查看
一、线程实现,代码如下:

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("秒表启动");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: