用线程和接口两种方法实现的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("秒表启动"); } }
相关文章推荐
- 2015070207 - EffactiveJava笔记 - 第50条 尽量避免使用字符串(1)
- java学习日记
- Java文件操作——IO流
- Java基础之面向对象思想的重要性
- Java基础总结之集合
- struts自定义拦截器的三种方式
- 2015070205 - EffactiveJava笔记 - 第49条 基本类型优于装箱类型(4)
- 2015第27周三Java内存模型
- Eclipse for Mac 常用快捷
- java求两个数的最大公约数和最小公倍数
- Spring整合Hibernate问题
- 《Java课程实习》日志(周四)
- 用线程和借口分别实现的Java秒表
- JAVA学习之面向对象编程
- java时间处理工具类--DateUtils
- Java程序设计第十七周之猜猜看
- java时间处理工具类--DateUtils
- 图说——JavaIO
- JAVAOO学习第三天(15-7-2)
- Java课程设计实训第四天——实现猜猜看游戏的达成