Java并发编程:线程的创建
Java并发编程:线程的创建
*/-->
code {color: #FF0000}
pre.src {background-color: #002b36; color: #839496;}
Java并发编程:线程的创建
Table of Contents
在Java中线程的创建主要有两种,一种是通过继承抽象类Thread,一种是通过实现Runnable接口。当然,还有Concurent包里面的Callable和Future也可以算是一种。
1 Thread
我们先来看一下,使用Thread如何创建线程:
public class ThreadDemo extends Thread {
@Override
public void run() {
System.out.println("Current thread name is: " + Thread.currentThread().getName());
} public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new ThreadDemo();
thread.start();
}
}
}
Current thread name is: Thread-0
Current thread name is: Thread-2
Current thread name is: Thread-1
Current thread name is: Thread-3
Current thread name is: Thread-5
Current thread name is: Thread-4
Current thread name is: Thread-6
Current thread name is: Thread-7
Current thread name is: Thread-8
Current thread name is: Thread-9
可以看到,我们创建来10个线程,但是执行的顺序是不确定的。可以设置优先级,默认是5,最大是10,最小是1.但是即使设置来优先级,顺序也是不能保证。
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new ThreadDemo();
thread.setPriority(i + 1);
thread.start();
}
}
优先级逐渐上升,但执行但结果还是一样。
2 Runnable
我们再来使用Runnable创建线程:
public class RunnableDemo implements Runnable {
@Override
public void run() {
System.out.println("Current runnable thread name is: " + Thread.currentThread().getName());
} public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new RunnableDemo());
thread.start();
}
}
}
Current runnable thread name is: Thread-0
Current runnable thread name is: Thread-3
Current runnable thread name is: Thread-2
Current runnable thread name is: Thread-5
Current runnable thread name is: Thread-1
Current runnable thread name is: Thread-6
Current runnable thread name is: Thread-7
Current runnable thread name is: Thread-4
Current runnable thread name is: Thread-8
Current runnable thread name is: Thread-9
可以看出,实现Runnable接口接口之后但RunnableDemo类但实例还是无法直接运行的,它必须将实例对象传入Thread类,然后,才能调用Thread对象中的start()进行启动。
3 start() 和 run()
我们接下来来看一下start() 和 run()的区别:
当我们中定义新的线程类的时候,唯一覆写的方法就是run()。那么,我们能不能直接调用run()方法呢?
答案是肯定的。
我们将start()替换为run()再试一下:
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new RunnableDemo());
thread.run();
}
}
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
结果全是主线程在运行,也就是说根据就没有新的线程启动运行。所以,使用run()方法调用时,就和一般的函数调用一样,是由当前线程进行调用的,并不会启动新的线程,然后在新的线程中运行这个run()方法。只有使用start()方法去调用,才会启动新的线程,然后,在新的线程中运行run()方法。
Date: 2017-07-04 21:37
Author: WEN YANG
Created: 2017-07-04 Tue 22:44
最新文章
- java 将数据写进文件
- Symantec Backup Exec 报";Access denied to directory xxx"; Error Code E0008488
- android笔记:DatePickerDialog日期设置对话框
- Swift -- 官方文档Swift-Guides的学习笔记
- spring mvc和spring配置扫描包问题
- 解决Ecshop因为动态ip问题登录后台自动退出
- android读取远程图片案例
- Hibernate学习之hql查询语句
- [转]C#自定义开关按钮控件--附带第一个私活项目截图
- fragment类onresume里面刷新操作处理
- 9、JcomboBox下拉框事件监听
- 模板C++ 03图论算法 2最短路之全源最短路(Floyd)
- if和for的应用
- QT 操作 excel 教程
- nginx反向代理二级域名注意事项
- 虎牙直播弹幕转换字幕格式 基于Node.js 的 huya-danmu
- Ubantu更新hostname &; hosts
- Linux -- Xshell ,Xftp远程连接中文乱码怎么解决?
- 获取JS数组中所有重复元素
- Quartz代码及配置详解(转)