多线程:创建线程的两种方式:
Thread thread=new Thread(); thread.run();//直接创建一个Thread对象,在run方法中实现操作;
Thread thread=new Thread(){
@Override
public void run(){Thread.currentThread.getName();//该方法获取到当前线程}
}//创建一个Thread类的子类,在子类中红重写run方法。
Thread thread=new Thread(new Runnable(){
@Override
public void run(){
try{
Thread.sleep(20000);
}catch(InterruptedException e){
e.printStackTrace();
}
输出:Thread.currentThread.getName();
});
thread.run();
一个Thread中若有一个Runable实现了run方法,Thread自己也实现了run方法。那么首先调用Thread自己的run方法,(如果Thread自己没有实现run方法,才会去找父类的run方法,也就是Runable中的run方法)
定时器:可以查看Timer中的方法,可以实现闹钟或者经过多久之后进行动作的操作。可以一个Timer中嵌套一个TimerTask,TimerTask可以自己实现自己的循环。时间可以自己设置。
有一个Quartz工具可以使用,定时发送邮件等功能。