创建线程方式
2015-08-25 22:38
190 查看
创建线程的2中方式:重写Thread的Run方法、将Runnable作为参数传递给Thread
public class TranditionalThread {
/**
* @param args
*/
public static void main(String[] args) {
//创建线程方式1
Thread thread = new Thread(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1:" + Thread.currentThread());
}
}
};
thread.start();
//创建线程方式2
Thread thread2 = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1:" + Thread.currentThread());
}
}
});
thread2.start();
}
}
public class TranditionalThread {
/**
* @param args
*/
public static void main(String[] args) {
//创建线程方式1
Thread thread = new Thread(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1:" + Thread.currentThread());
}
}
};
thread.start();
//创建线程方式2
Thread thread2 = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1:" + Thread.currentThread());
}
}
});
thread2.start();
}
}
相关文章推荐
- 字符串,基本数据类型对象包装类
- 使用pdb/日志/gdb调试Python http://blog.csdn.net/atfield/article/details/1396735
- POJ-2559-Largest Rectangle in a Histogram-单调栈
- Java两整数相除保留两位小数
- 首先爱自己,然后再爱别人
- Java两整数相除保留两位小数
- Java两整数相除保留两位小数
- HTML5 MUI 手机预览图片,裁剪上传base64,保存数据库
- leetcode 线性表
- 质数筛 nbut1454 Lux
- 避免重复
- 分布式系统的特点以及设计理念
- group by rollup用法
- 排序算法
- UI_UIImageViw(图片)
- 【一些事晚报】互联网行业那些土豪岗位工资有多高
- Eclipse环境下的Python编程
- 锤子---坚果智能手机发布观感
- 现代心理与教育统计学 第一章
- 如何让div水平垂直居中