1.继承Thread类
public class Mythread extends Thread{
public void run(){
//执行的方法体
System.out.println("mythread is running...");
}
public static void main(String[] args){
Mythread t1=new Mythread();
t1.start();
}
}
2.实现Runnable接口
public class Mythread implements Runnable{
public void run(){
//执行的方法体
System.out.println("mythread is running...");
}
public static void main(String[] args){
Mythread t1=new Mythread();
Thread t2=new Thread(t1);
// 或者 Thread t2=new Thread(new Mythread());
t2.start();
}
}
3.通过Callable和FutrueTask类实现
public class Mythread implements Callable{
public Object call(){
//执行的方法体,返回值通过t2.get得到
return null;
}
public static void main(String[] args){
Mythread t1=new Mythread();
FutureTask t2=new FutureTask(t1);
Thread t3=new Thread(t2);
t3.start();
}
}
4.通过线程池来创建线程
Executors.newSingleThreadExecutor();//创建容量为1的缓冲池