多线程
一、多线程的三种创建方式
1、通过继承Thread类
************************************************
public class duoxiancheng extends Thread{
public void run(){
//创建一个无线循环(但是加入了时间间隔)
while(true){
/**
*
*
* 具体内容代码
*
*
*/
//需要加入异常处理机制
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
************************************************
duoxiancheng th = new duoxiancheng();
th.start();
2、通过实现Runnable接口
************************************************
public class duoxiancheng implements Runnable{
public void run(){
//创建一个无线循环(但是加入了时间间隔)
while(true){
/**
*
*
* 具体内容代码
*
*
*/
//需要加入异常处理机制
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
************************************************
duoxiancheng rt = new duoxiancheng();
Thread thread = new Thread(rt);
thread.start();
3、通过匿名内部类
【略】