我们知道,我们通过调用线程的start方法启动一个线程,那么,我们可以直接调用run方法来启动一个线程吗?

先看下面一段代码:

  1. public class Test {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. TestThread tt = new TestThread();
  5. tt.run();
  6. }
  7. }
  8. class TestThread extends Thread {
  9. static int i = 0;
  10. final static int MAX_I = 10;
  11. @Override
  12. public void run() {
  13. // TODO Auto-generated method stub
  14. while (i < MAX_I) {
  15. System.out.println(i++);
  16. }
  17. }
  18. }

运行结果如下:

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9

或许有人会得出结论,这样启动一个线程是可以的,我们再对程式稍做修改,大家就会发现一个问题:

  1. public class Test {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. TestThread tt = new TestThread();
  5. tt.run();
  6. System.out.println("Printed by main thread");
  7. }
  8. }
  9. class TestThread extends Thread {
  10. static int i = 0;
  11. final static int MAX_I = 10;
  12. @Override
  13. public void run() {
  14. // TODO Auto-generated method stub
  15. while (i < MAX_I) {
  16. System.out.println(i++);
  17. }
  18. }
  19. }

这里只在主线程中加入了一行代码,打印一行"Printed by main thread",运行代码,结果如下:

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. Printed by main thread

熟练多线程开发的要发现问题了,为什么"Printed by main thread"会打印在最后一行呢?TestThread类中一直持有时间段吗?

我们对上面的代码进行分析,其实非常简单,这只是一个普通的类中方法的调用,其实是一个单线程的执行,我们来修改代码进一步验证这一点:

  1. public class Test {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. TestThread tt = new TestThread();
  5. tt.run();
  6. System.out.println(Thread.currentThread().getName());
  7. System.out.println("Printed by main thread");
  8. }
  9. }
  10. class TestThread extends Thread {
  11. static int i = 0;
  12. final static int MAX_I = 10;
  13. @Override
  14. public void run() {
  15. // TODO Auto-generated method stub
  16. System.out.println(Thread.currentThread().getName());
  17. while (i < MAX_I) {
  18. System.out.println(i++);
  19. }
  20. }
  21. }

这段代码分别在主线程和我们的TestThread的方法中打印当前线程名字,运行结果如下:

  1. main
  2. 0
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 6
  9. 7
  10. 8
  11. 9
  12. main
  13. Printed by main thread

在TestThread类和主线程中运行的是同一个线程,说明在直接调用run时是不能使用多线程的,那么把上面的run方法调用改为start方法的调动再看一下:

  1. public class Test {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. TestThread tt = new TestThread();
  5. tt.start();
  6. System.out.println(Thread.currentThread().getName());
  7. System.out.println("Printed by main thread");
  8. }
  9. }
  10. class TestThread extends Thread {
  11. static int i = 0;
  12. final static int MAX_I = 10;
  13. @Override
  14. public void run() {
  15. // TODO Auto-generated method stub
  16. System.out.println(Thread.currentThread().getName());
  17. while (i < MAX_I) {
  18. System.out.println(i++);
  19. }
  20. }
  21. }

运行结果如下:

  1. main
  2. Thread-0
  3. 0
  4. 1
  5. 2
  6. 3
  7. 4
  8. 5
  9. 6
  10. 7
  11. 8
  12. Printed by main thread
  13. 9

很明显,这才是我们想看到的结果,所以结论是只有调用Thread的start方法,将线程交由JVM控制,才能产生多线程,而直接调用run方法只是一个普通的单线程程式。

Java start和run启动线程的区别的更多相关文章

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

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

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

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

  3. java Thread 类 run 和 start 方法区别

    public class ThreadModle { public static void main(String[] args) throws InterruptedException { Thre ...

  4. Java中Thread方法启动线程

    public class ThreadTest extends Thread {  private int count = 10; @Override public void run() { //重写 ...

  5. Java Thread 的 sleep() 和 wait() 的区别

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

  6. Java线程Run和Start的区别

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

  7. 启动线程,start和run的区别

    每个线程都有要执行的任务.线程的任务处理逻辑可以在Tread类的run实例方法中直接实现或通过该方法进行调用,因此 run()相当于线程的任务处理逻辑的入口方法,它由Java虚拟机在运行相应线程时直接 ...

  8. JAVA面试题 启动线程是start()还是run()?为什么?

    面试官:请问启动线程是start()还是run()方法,能谈谈吗? 应聘者:start()方法 当用start()开始一个线程后,线程就进入就绪状态,使线程所代表的虚拟处理机处于可运行状态,这意味着它 ...

  9. java核心知识点学习----并发和并行的区别,进程和线程的区别,如何创建线程和线程的四种状态,什么是线程计时器

    多线程并发就像是内功,框架都像是外功,内功不足,外功也难得精要. 1.进程和线程的区别 一个程序至少有一个进程,一个进程至少有一个线程. 用工厂来比喻就是,一个工厂可以生产不同种类的产品,操作系统就是 ...

随机推荐

  1. 【二叉树的递归】07路径组成数字的和【Sum Root to Leaf Numbers】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,节点的值仅限于从0 ...

  2. 20179215《Linux内核原理与分析》第一周作业

    一.Linux介绍 我们现在很常见Windows系统,对于Linux则显得尤为陌生.当然我也不例外,初识Linux过程中遇到一些困惑,但我也在实验的同时通过不断查找资料与实践中慢慢解决问题.那么下面我 ...

  3. cookie与web Storage

    一.cookie 1. http头与cookie cookie是HTTP Cookie的简称.该标准要求: (1)服务器的HTTP响应头包含 Set-Cookie字段 响应头Eg: HTTP/1.1 ...

  4. HDOJ1151有向图最小路径覆盖

    //有向图最小路径覆盖:从某一点出发沿着有向路径,不走回路,能将所有的结点遍历. #include<iostream> #include<cstdio> #include< ...

  5. 3 K8s安裝ELK+filebeat

    1 Filebeat: apiVersion: v1 kind: Service metadata: name: XX spec: ports: - name: http port: targetPo ...

  6. Ok6410裸机驱动学习(二)ARM基础知识

    1.ARM工作模式 ARM微处理器支持7种工作模式,分别为: l  用户模式(usr):ARM处理器正常的程序执行状态(Linux用户态程序) l  快速中断模式(fiq):用于高速数据传输或通道处理 ...

  7. how to download a file with Nodejs(without using third-party libraries)用node下载文件

    创建HTTP GET请求并将其管理response到可写文件流中: var http = require('http'); var fs = require('fs'); var file = fs. ...

  8. 具体问题:Spring 事务的隔离性,并说说每个隔离性的区别

    使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...

  9. Hive中SELECT TOP N的方法(order by与sort by的区别)

    我想说的SELECT TOP N是取最大前N条或者最小前N条. Hive提供了limit关键字,再配合order by可以很容易地实现SELECT TOP N. 但是在Hive中order by只能使 ...

  10. 在linux系统个人目录下安装新版python

    一.下载所需的python版本文件 在linux个人目录下 下载https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz 解压tar.xz ...