您的位置:首页 > 其它

3种实现线程的方法

2013-09-16 19:49 148 查看
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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: