3种实现线程的方法
2013-09-16 19:49
190 查看
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();
相关文章推荐
- 3种线程的实现方法安卓开发
- 实现跨线程访问UI控件的3种方法
- 实现一个线程的两种方法【转载】
- JavaScript实现判断图片是否加载完成的3种方法整理
- Android中定时器的3种实现方法
- Android中定时执行任务的3种实现方法
- Apache shiro集群实现 (八) web集群时session同步的3种方法
- PHP使用3种方法实现数据采集
- javascript中的3种继承实现方法
- Javascript中3种实现继承的方法和代码实例
- php实现返回上一页的功能的3种有效方法
- Android—— 线程 thread 两种实现方法!(转)
- CRC算法原理及C语言实现(介绍了3种方法)
- Android中定时执行任务的3种实现方法
- 嵌入式实时操作系统Nucleus中线程控制部件的实现方法
- java实现二叉树的构建以及3种遍历方法
- Android中定时器的3种实现方法
- 实现View滑动的3种方法
- Android中定时器的3种实现方法