1) start:

用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面(指主线程下面)的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。

 package com.mianshi.easy;
public class StartRunTest {
/**
* 直接调用run()和用start()启动一个线程的差别
*/ public static void main(String[] args){
Thread thread=new ThreadDemo();
//第一种
//表明: run()和其他方法的调用没任何不同,main方法按顺序执行了它,并打印出最后一句
/*thread.run();
for(int i=0;i<100;i++){
System.out.println(i);
}*/ //第二种
//表明: start()方法重新创建了一个线程,在main方法执行结束后,由于start()方法创建的线程没有运行结束,
//因此主线程未能退出,直到线程thread也执行完毕.这里要注意,默认创建的线程是用户线程(非守护线程)
thread.start();
for(int i=0;i<100;i++){
System.out.println(i);
} //第三种
//1、为什么没有打印出100句呢?因为我们将thread线程设置为了daemon(守护)线程,程序中只有守护线程存在的时候,是可以退出的,所以只打印了七句便退出了
//2、当java虚拟机中有守护线程在运行的时候,java虚拟机会关闭。当所有常规线程运行完毕以后,
//守护线程不管运行到哪里,虚拟机都会退出运行。所以你的守护线程最好不要写一些会影响程序的业务逻辑。否则无法预料程序到底会出现什么问题
/*thread.setDaemon(true);
thread.start();*/ //第四种
//用户线程可以被System.exit(0)强制kill掉,所以也只打印出七句
/* thread.start();
System.out.println("main thread is over");
System.exit(1);*/
} public static class ThreadDemo extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("This is a Thread test"+i);
}
}
}
}

结果:

0
1
2
3
4
5
6
7
This is a Thread test0
This is a Thread test1
This is a Thread test2
This is a Thread test3
This is a Thread test4
8
This is a Thread test5
This is a Thread test6
This is a Thread test7
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
This is a Thread test8
This is a Thread test9
This is a Thread test10
This is a Thread test11
This is a Thread test12
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
This is a Thread test13
78
This is a Thread test14
This is a Thread test15
This is a Thread test16
This is a Thread test17
This is a Thread test18
This is a Thread test19
This is a Thread test20
This is a Thread test21
This is a Thread test22
This is a Thread test23
This is a Thread test24
This is a Thread test25
This is a Thread test26
This is a Thread test27
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
This is a Thread test28
This is a Thread test29
This is a Thread test30
This is a Thread test31
This is a Thread test32
This is a Thread test33
This is a Thread test34
This is a Thread test35
This is a Thread test36
This is a Thread test37
This is a Thread test38
This is a Thread test39
This is a Thread test40
This is a Thread test41
This is a Thread test42
This is a Thread test43
This is a Thread test44
This is a Thread test45
This is a Thread test46
This is a Thread test47
This is a Thread test48
This is a Thread test49
This is a Thread test50
This is a Thread test51
This is a Thread test52
This is a Thread test53
This is a Thread test54
This is a Thread test55
This is a Thread test56
This is a Thread test57
This is a Thread test58
This is a Thread test59
This is a Thread test60
This is a Thread test61
This is a Thread test62
This is a Thread test63
This is a Thread test64
This is a Thread test65
This is a Thread test66
This is a Thread test67
This is a Thread test68
This is a Thread test69
This is a Thread test70
This is a Thread test71
This is a Thread test72
This is a Thread test73
This is a Thread test74
This is a Thread test75
This is a Thread test76
This is a Thread test77
This is a Thread test78
This is a Thread test79
This is a Thread test80
This is a Thread test81
This is a Thread test82
This is a Thread test83
This is a Thread test84
This is a Thread test85
This is a Thread test86
This is a Thread test87
This is a Thread test88
This is a Thread test89
This is a Thread test90
This is a Thread test91
This is a Thread test92
This is a Thread test93
This is a Thread test94
This is a Thread test95
This is a Thread test96
This is a Thread test97
This is a Thread test98
This is a Thread test99

子线程成功创建,主线程的代码也在继续运行,而且证明了子线程短暂等待CPU时间片的过程,此过程中主线程没有停止。

2) run:

run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。

 package com.mianshi.easy;
public class StartRunTest {
/**
* 直接调用run()和用start()启动一个线程的差别
*/ public static void main(String[] args){
Thread thread=new ThreadDemo();
//第一种
//表明: run()和其他方法的调用没任何不同,main方法按顺序执行了它,并打印出最后一句
thread.run();
for(int i=0;i<100;i++){
System.out.println(i);
} //第二种
//表明: start()方法重新创建了一个线程,在main方法执行结束后,由于start()方法创建的线程没有运行结束,
//因此主线程未能退出,直到线程thread也执行完毕.这里要注意,默认创建的线程是用户线程(非守护线程)
/*thread.start();
for(int i=0;i<100;i++){
System.out.println(i);
}*/ //第三种
//1、为什么没有打印出100句呢?因为我们将thread线程设置为了daemon(守护)线程,程序中只有守护线程存在的时候,是可以退出的,所以只打印了七句便退出了
//2、当java虚拟机中有守护线程在运行的时候,java虚拟机会关闭。当所有常规线程运行完毕以后,
//守护线程不管运行到哪里,虚拟机都会退出运行。所以你的守护线程最好不要写一些会影响程序的业务逻辑。否则无法预料程序到底会出现什么问题
/*thread.setDaemon(true);
thread.start();*/ //第四种
//用户线程可以被System.exit(0)强制kill掉,所以也只打印出七句
/* thread.start();
System.out.println("main thread is over");
System.exit(1);*/
} public static class ThreadDemo extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("This is a Thread test"+i);
}
}
}
}

结果:

This is a Thread test0
This is a Thread test1
This is a Thread test2
This is a Thread test3
This is a Thread test4
This is a Thread test5
This is a Thread test6
This is a Thread test7
This is a Thread test8
This is a Thread test9
This is a Thread test10
This is a Thread test11
This is a Thread test12
This is a Thread test13
This is a Thread test14
This is a Thread test15
This is a Thread test16
This is a Thread test17
This is a Thread test18
This is a Thread test19
This is a Thread test20
This is a Thread test21
This is a Thread test22
This is a Thread test23
This is a Thread test24
This is a Thread test25
This is a Thread test26
This is a Thread test27
This is a Thread test28
This is a Thread test29
This is a Thread test30
This is a Thread test31
This is a Thread test32
This is a Thread test33
This is a Thread test34
This is a Thread test35
This is a Thread test36
This is a Thread test37
This is a Thread test38
This is a Thread test39
This is a Thread test40
This is a Thread test41
This is a Thread test42
This is a Thread test43
This is a Thread test44
This is a Thread test45
This is a Thread test46
This is a Thread test47
This is a Thread test48
This is a Thread test49
This is a Thread test50
This is a Thread test51
This is a Thread test52
This is a Thread test53
This is a Thread test54
This is a Thread test55
This is a Thread test56
This is a Thread test57
This is a Thread test58
This is a Thread test59
This is a Thread test60
This is a Thread test61
This is a Thread test62
This is a Thread test63
This is a Thread test64
This is a Thread test65
This is a Thread test66
This is a Thread test67
This is a Thread test68
This is a Thread test69
This is a Thread test70
This is a Thread test71
This is a Thread test72
This is a Thread test73
This is a Thread test74
This is a Thread test75
This is a Thread test76
This is a Thread test77
This is a Thread test78
This is a Thread test79
This is a Thread test80
This is a Thread test81
This is a Thread test82
This is a Thread test83
This is a Thread test84
This is a Thread test85
This is a Thread test86
This is a Thread test87
This is a Thread test88
This is a Thread test89
This is a Thread test90
This is a Thread test91
This is a Thread test92
This is a Thread test93
This is a Thread test94
This is a Thread test95
This is a Thread test96
This is a Thread test97
This is a Thread test98
This is a Thread test99
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

并没有产生子线程,而是普通的方法调用,只有等调用方法执行完后,主线程才能继续执行处后面的代码。

总结:调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。

参考:http://visionsky.blog.51cto.com/733317/431397

run() 和 start() 的区别的更多相关文章

  1. Java Thread 的 run() 与 start() 的区别

    Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别             1. ...

  2. delphi Syntax check、 build、 run、 compile的区别

    delphi Syntax check. build.  run. compile的区别 Build是从新编译所有和生成exe有关的文件,无论.pas文件是否修改过,它都会重新生成新的.dcu,并从新 ...

  3. 多线程-Thread的run()与start()的区别

    总结: 1) start: 用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码.通过调用Thread类的start()方法来启动一个线程,这 ...

  4. Java线程Run和Start的区别

    先上结论:run只是Thread里面的一个普通方法,start是启动线程的方法.何以见得呢?可以执行下面的代码看看run和start的区别: package com.basic.thread; /** ...

  5. Thread和Runnable、run和start的区别

    多线程可以通过两种方式来创建: 一.通过继承Thread类. 二.通过实现Runnable接口. 那么中两种方式到底有什么区别呢?那种方式更好些呢? 先看看几个简单的Demo: Demo1 publi ...

  6. Dockerfile 中 RUN, CMD, ENTRYPOINT 的区别

    RUN 指令:用于指定 docker build 过程中要运行的命令. 语法格式: RUN <command> 或 RUN ["<executeable>" ...

  7. Java并发编程:Java Thread 的 run() 与 start() 的区别

    1. sleep 和 wait 方法解释 sleep()方法是Thread类里面的,主要的意义就是让当前线程停止执行,让出cpu给其他的线程,但是不会释放对象锁资源以及监控的状态,当指定的时间到了之后 ...

  8. tensorflow中run和eval的区别(转)

    在tensorflow中,eval和run都是获取当前结点的值的一种方式. 在使用eval时,若有一个 t 是Tensor对象,调用t.eval()相当于调用sess.run(t) 一下两段代码等效: ...

  9. java面试题之Thread类中的start()和run()方法有什么区别

    start()方法被用来启动新创建的线程,而且start()内部调用了run()方法, 区别: 当你调用run()方法的时候,只会是在原来的线程中调用,没有新的线程启动: start()方法才会启动新 ...

随机推荐

  1. 记录一个mysql连接慢的问题

    问题现象是这样的: 我在一台机器上(61.183.23.23)启动了一个mysql,然后开通一个账号可以从127.0.0.1或者从61.183.23.23访问.但是遇到一个问题就是使用下面两个命令行访 ...

  2. 基于HT for Web矢量实现3D叶轮旋转

    在上一篇<基于HT for Web矢量实现2D叶轮旋转>中讲述了叶轮旋转在2D上的应用,今天我们就来讲讲叶轮旋转在3D上的应用. 在3D拓扑上可以创建各种各样的图元,在HT for Web ...

  3. ::selection{}

    ::selectiion{}这是一个伪类选择器,这个是当我们在选择网页上某些文字时(通常是为了复制文字),文字的背景颜色和文字颜色都会发生变化(一般为背景变成蓝色,文字变成白色),这样的写法在IE9及 ...

  4. WPF学习之绘图和动画

    如今的软件市场,竞争已经进入白热化阶段,功能强.运算快.界面友好.Bug少.价格低都已经成为了必备条件.这还不算完,随着计算机的多媒体功能越来越强,软件的界面是否色彩亮丽.是否能通过动画.3D等效果是 ...

  5. NoSQL数据库介绍

    NoSQL在2010年风生水起,大大小小的Web站点在追求高性能高可靠性方面,不由自主都选择了NoSQL技术作为优先考虑的方面.今年伊始,InfoQ中文站有幸邀请到凤凰网的孙立先生,为大家分享他之于N ...

  6. Java连接MYSQL 数据库的连接步骤

    这篇文章主要以MySQL为例讲下Java如何连接到数据库的. 当然,首先要安装有JDK(一般是JDK1.5.X).然后安装MySQL,这些都比较简单,具体过程就不说了.配置好这两个环境后,下载JDBC ...

  7. FireMonkey 导出目前 Style 另存文件

    FireMonkey 能将目前使用的 Style 导出成文件,它提供二种文件格式,请看下列代码: *.style procedure TForm1.Button1Click(Sender: TObje ...

  8. 一个奇葩常见的问题 nginx 403 forbidden错误

    今天安装dedecms,配置Nginx,然后生成一键生成静态页面,然后就没有然后了,所有栏目页面都显示nginx 403 forbidden. 一般来说nginx 的 403 Forbidden er ...

  9. sql2000安装的一般问题

    SQLServer2000 在一段时间不使用后突然间不能够运行了.只能打开企业管理器,对数据库进行操作.VS2005不能够连接,试了很多种方式,无结果.于是重新安装 sqlServer2000? 仿真 ...

  10. JavaScript基础------入门基础

    JavaScript他是一种描述性语言,使用JavaScript就是为了能和网页有更好的交互,下面切入主题进行讲解. 一.JavaScript 1.什么是JavaScript JavaScript是一 ...