Java start和run启动线程的区别
2016-05-24 08:28
531 查看
我们知道,我们通过调用线程的start方法启动一个线程,那么,我们可以直接调用run方法来启动一个线程吗?
先看下面一段代码:
[java] view plain copy
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread tt = new TestThread();
tt.run();
}
}
class TestThread extends Thread {
static int i = 0;
final static int MAX_I = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while (i < MAX_I) {
System.out.println(i++);
}
}
}
运行结果如下:
[java] view plain copy
0
1
2
3
4
5
6
7
8
9
或许有人会得出结论,这样启动一个线程是可以的,我们再对程式稍做修改,大家就会发现一个问题:
[java] view plain copy
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread tt = new TestThread();
tt.run();
System.out.println("Printed by main thread");
}
}
class TestThread extends Thread {
static int i = 0;
final static int MAX_I = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while (i < MAX_I) {
System.out.println(i++);
}
}
}
这里只在主线程中加入了一行代码,打印一行"Printed by main thread",运行代码,结果如下:
[xhtml] view plain copy
0
1
2
3
4
5
6
7
8
9
Printed by main thread
熟练多线程开发的要发现问题了,为什么"Printed by main thread"会打印在最后一行呢?TestThread类中一直持有时间段吗?
我们对上面的代码进行分析,其实非常简单,这只是一个普通的类中方法的调用,其实是一个单线程的执行,我们来修改代码进一步验证这一点:
[java] view plain copy
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread tt = new TestThread();
tt.run();
System.out.println(Thread.currentThread().getName());
System.out.println("Printed by main thread");
}
}
class TestThread extends Thread {
static int i = 0;
final static int MAX_I = 10;
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
while (i < MAX_I) {
System.out.println(i++);
}
}
}
这段代码分别在主线程和我们的TestThread的方法中打印当前线程名字,运行结果如下:
[xhtml] view plain copy
main
0
1
2
3
4
5
6
7
8
9
main
Printed by main thread
在TestThread类和主线程中运行的是同一个线程,说明在直接调用run时是不能使用多线程的,那么把上面的run方法调用改为start方法的调动再看一下:
[java] view plain copy
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread tt = new TestThread();
tt.start();
System.out.println(Thread.currentThread().getName());
System.out.println("Printed by main thread");
}
}
class TestThread extends Thread {
static int i = 0;
final static int MAX_I = 10;
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
while (i < MAX_I) {
System.out.println(i++);
}
}
}
运行结果如下:
[xhtml] view plain copy
main
Thread-0
0
1
2
3
4
5
6
7
8
Printed by main thread
9
很明显,这才是我们想看到的结果,所以结论是只有调用Thread的start方法,将线程交由JVM控制,才能产生多线程,而直接调用run方法只是一个普通的单线程程式。
先看下面一段代码:
[java] view plain copy
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread tt = new TestThread();
tt.run();
}
}
class TestThread extends Thread {
static int i = 0;
final static int MAX_I = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while (i < MAX_I) {
System.out.println(i++);
}
}
}
运行结果如下:
[java] view plain copy
0
1
2
3
4
5
6
7
8
9
或许有人会得出结论,这样启动一个线程是可以的,我们再对程式稍做修改,大家就会发现一个问题:
[java] view plain copy
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread tt = new TestThread();
tt.run();
System.out.println("Printed by main thread");
}
}
class TestThread extends Thread {
static int i = 0;
final static int MAX_I = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while (i < MAX_I) {
System.out.println(i++);
}
}
}
这里只在主线程中加入了一行代码,打印一行"Printed by main thread",运行代码,结果如下:
[xhtml] view plain copy
0
1
2
3
4
5
6
7
8
9
Printed by main thread
熟练多线程开发的要发现问题了,为什么"Printed by main thread"会打印在最后一行呢?TestThread类中一直持有时间段吗?
我们对上面的代码进行分析,其实非常简单,这只是一个普通的类中方法的调用,其实是一个单线程的执行,我们来修改代码进一步验证这一点:
[java] view plain copy
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread tt = new TestThread();
tt.run();
System.out.println(Thread.currentThread().getName());
System.out.println("Printed by main thread");
}
}
class TestThread extends Thread {
static int i = 0;
final static int MAX_I = 10;
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
while (i < MAX_I) {
System.out.println(i++);
}
}
}
这段代码分别在主线程和我们的TestThread的方法中打印当前线程名字,运行结果如下:
[xhtml] view plain copy
main
0
1
2
3
4
5
6
7
8
9
main
Printed by main thread
在TestThread类和主线程中运行的是同一个线程,说明在直接调用run时是不能使用多线程的,那么把上面的run方法调用改为start方法的调动再看一下:
[java] view plain copy
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread tt = new TestThread();
tt.start();
System.out.println(Thread.currentThread().getName());
System.out.println("Printed by main thread");
}
}
class TestThread extends Thread {
static int i = 0;
final static int MAX_I = 10;
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
while (i < MAX_I) {
System.out.println(i++);
}
}
}
运行结果如下:
[xhtml] view plain copy
main
Thread-0
0
1
2
3
4
5
6
7
8
Printed by main thread
9
很明显,这才是我们想看到的结果,所以结论是只有调用Thread的start方法,将线程交由JVM控制,才能产生多线程,而直接调用run方法只是一个普通的单线程程式。
相关文章推荐
- MyEclipse解决中文乱码问题,统一为UTF-8,及其他设置
- Java Web项目实战,Jsp项目实战
- AsMVC:一个简单的MVC框架的Java实现
- Spring MVC示例及常用注解(二)
- Stream常用操作min,reduce
- servlet监听器
- Spring MVC绑定 List 对象参数
- [Eclipse] - 解决导入flask模块出现的Unresolved Import flask问题
- Java 通过JDBC连接Mysql数据库
- commons-lang3
- Java基础:循环迭代Array/List/Set/Map集合 增强for循环
- java泛型学习-自定义泛型方法与类型推断总结
- JAVA中list,set,数组之间的转换详解
- java 变量的定义在程序中是任意的
- BigDecimal 高精度计算 熟悉扩展,java除法保留小数问题
- Realm Java的学习、应用、总结
- Java-反射机制reflect
- Java学习之路(第一天)
- MyEclipse功能
- springMVC(13)------springMVC静态资源的访问