实现多线程编程有两种方式
1.继承Thread方式
2.实现Runnable方式
因为java是单继承的关系,所以实现Runnable接口方式实现多线程方式,有效的解决了这一麻烦事。
public class ShowTime implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
for (;;) {
Thread.sleep(1000);
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");//时间格式匹配
String timeString=format.format(new Date());
System.out.println(timeString);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
主函数声明线程调用方法
public class ShowTimeTest {
public static void main(String[] args) {
ShowTime showTime = new ShowTime();
Thread thread = new Thread(showTime);
thread.start();
}
}