创建线程的两种方式
来源:互联网 发布:骑行眼镜 知乎 编辑:程序博客网 时间:2024/05/16 15:08
方式一:继承Thread
/**
*
* @author Alan
*继承Thread类创建新线程:
*定义线程:写一个类继承java.lang.Thread类,重写run方法。
*创建线程实例:创建上一步定义的类的实例即可。
*/
public class ThreadCreate1 {
public static void main(String[] args) {
//2创建线程实例
Thread thread1=new MyThread();
thread1.start();
}
}
//1定义线程
class MyThread extends Thread{
@Override
public void run() {
for(int i=0; i<5; i++){
System.out.println(Thread.currentThread().getName()+i);
}
}
}
方式二:实现Runable接口 (推荐方式)/**
*
* @author Alan
*实现Runnable接口创建新线程:
*定义Runnable:写一个类实现Runnable
*创建线程实例:Runnable对象实例传入构造函数
*/
public class ThreadCreate2 {
public static void main(String[] args) {
//2创建Runnable对象实例
Runnable runner=new Runner2();
//3以Runnable对象实例为参数,用构造函数创建新线程
Thread thread2=new Thread(runner);
thread2.start();
}
}
//1定义Runable对象
class Runner2 implements Runnable{
public void run() {
for(int i=0; i<5; i++){
System.out.println(Thread.currentThread().getName()+i);
}
}
}
- 创建线程两种方式的比较
- java 创建线程 的两种方式
- 创建线程两种方式的比较
- 创建线程的两种方式
- 创建线程的两种方式
- 多线程 创建线程的两种方式
- 创建线程的两种方式
- Java线程创建的两种方式
- 创建线程的两种方式
- 创建线程的两种方式
- 创建线程的两种方式。
- java创建线程的两种方式
- 线程的两种创建方式
- java创建线程的两种方式
- java线程的两种创建方式
- Java创建线程的两种方式
- 线程创建的两种方式
- 创建线程的两种方式
- Cocos2D-X入门学习-环境搭建-创建新项目-01
- BigDecimal 处理高精度的浮点数
- Apache部署Django环境
- 2013-10-13 香港电子展 感言
- 错误 error C2065: 未声明的标识符 解决办法
- 创建线程的两种方式
- orcad 快捷键
- 50. 学会评估
- 安徽科大讯飞笔试题2014校园招聘 武汉
- struts2中多文件上传的处理
- 人人校园招聘笔试题 vs 算法导论练习题
- securecrt 的一些实用功能
- (转)教你如何监控你的妹子或者将来的妹子的手机
- Centos 6.4 minimal kamailio编译安装