1.通过继承Thread实现线程:
public class MyThread extends Thread{
public void run(){
//线程体
}
public static void main(String[] args){
MyThread myth1=new MyThread();
myth1.start();
}
}
2.通过实现Runnable实现线程:
public class RunnableDemo implements Runnbale{
public void run(){
//线程体
}
public static void main(String[] args){
Runnable runnable=new RunnableDemo();
Thread td=new Thread(runnable);
td.start();
}
}
或者:在主线程中通过匿名内部类(带参)来实现线程
new Thread(new Runnable(){
public void run(){
//线程体
}
}).start();
3.直接通过内部类(不带参数)来实现线程
new Thread(){
//线程体
}.start();
public class MyThread extends Thread{
public void run(){
//线程体
}
public static void main(String[] args){
MyThread myth1=new MyThread();
myth1.start();
}
}
2.通过实现Runnable实现线程:
public class RunnableDemo implements Runnbale{
public void run(){
//线程体
}
public static void main(String[] args){
Runnable runnable=new RunnableDemo();
Thread td=new Thread(runnable);
td.start();
}
}
或者:在主线程中通过匿名内部类(带参)来实现线程
new Thread(new Runnable(){
public void run(){
//线程体
}
}).start();
3.直接通过内部类(不带参数)来实现线程
new Thread(){
//线程体
}.start();