前提:有一个线程的执行主类
要实现一个多线程的主类:
每一个
1.继承一个Thread类:
继承Tread类并且覆写一个run方法
Example:
class MyThread extends Thread{
private String title;
public MyThread(String title){
this.title=title;
}
public void run(){
for(int i=0;i<10;i++){
System.out.println(title+" : "+i);
}
}
}
public class Test {
public static void main(String args[]) {
MyThread m1= new MyThread("Thread1");
MyThread m2= new MyThread("Thread2");
MyThread m3= new MyThread("Thread3");
m3.start();
m2.start();
m1.start();
}
}
2.(推荐)实现Runnable、Callable接口