/**
* @param 传统的线程的两张创建方法
*/
public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1:" + Thread.currentThread().getName());
}
}
};
thread.start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("2:" + Thread.currentThread().getName());
}
}
});
thread2.start();
}
/**
* @param 传统定时器
*/
public static void main(String[] args) {
new Timer().schedule(new TimerTask(){
@Override
public void run() {
System.out.println("执行...");
}
}, 3000, 1000);//第一次执行在3秒后,后面就每隔1秒执行一次
}