先说java中实现多线程常用的两种方式:
   1:继承Thread类,并重写run()方法
   2:实现Runnable接口,实现run方法
实际上Thread类也是实现了Runnable接口

[Java] 纯文本查看 复制代码
1
2
3
public class Thread implements Runnable {
  ........

而Runnable接口只定义了一个run方法

[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

我们需要实现的多线程业务逻辑,都需要在run方法内实现。

而start()是Thread中定义的方法,以下是start()方法的源码

[Java] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();
 
        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);
 
        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

start()通过调用本地native方法start0来启动一个线程,本地方法内最终会调用run方法,这样就可以实现在另一个线程中运行我们需要的代码。
而单独运行run()方法,就跟我们调用普通接口实现方法一样了,并不会启动新的线程.
总结:
1、start()方法运行时,本地启动新的线程,线程中会调用run方法,程序不需要等待run方法执行完毕再执行后面的程序;

2、run()方法是我们编写需要新的线程执行代码的方法,单独运行是不会有多线程效果的。

更多技术资讯可关注:itheimaGZ获取

线程中start和run方法的区别的更多相关文章

  1. Java -- Thread中start和run方法的区别

    一.认识Thread的 start() 和 run() 1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发 ...

  2. Java中start和run方法的区别

    一.问题引入         说到这两个方法就不得不说多线程,说到多线程就不得不提实现多线程的两种方式继承Thread类和实现Runable接口,下面先看这两种方式的区别. 二. Java中实现多线程 ...

  3. 认识多线程中start和run方法的区别?

    一.认识多线程中的 start() 和 run() 1.start(): 先来看看Java API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并 ...

  4. 线程的start和run方法的区别

    回到这个问题,可以用源码的角度去回答,这样会让面试官对有更好的印象 ------>如果直接调用run方法的话,所执行的线程是main线程.调用start方法的话,会新建一个子线程,去执行run方 ...

  5. Java线程中yield与join方法的区别

    长期以来,多线程问题颇为受到面试官的青睐.虽然我个人认为我们当中很少有人能真正获得机会开发复杂的多线程应用(在过去的七年中,我得到了一个机会),但是理解多线程对增加你的信心很有用.之前,我讨论了一个w ...

  6. 【Java_多线程并发编程】基础篇—Thread类中start()和run()方法的区别

    1. start() 和 run()的区别说明 start()方法: 它会启动一个新线程,并将其添加到线程池中,待其获得CPU资源时会执行run()方法,start()不能被重复调用. run()方法 ...

  7. 线程中start与run方法的主要区别

    区别一:                在于当程序调用start方法一个新线程将会被创建,并且在run方法中的代码将会在新线程上运行,                然而在你直接调用run方法的时候, ...

  8. 线程中sleep和wait方法的区别

    sleep() 方法: 线程主动放弃CPU,使得线程在指定的时间内进入阻塞状态,不能得到CPU 时间,指定的时间一过,线程重新进入可执行状态.典型地,sleep()被用在等待某个资源就绪的情形:测试发 ...

  9. 线程的状态有哪些,线程中的start与run方法的区别

    线程在一定条件下,状态会发生变化.线程一共有以下几种状态: 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法. ...

随机推荐

  1. jQuery 1.3.2 简单实现select二级联动

    jQuery 1.3.2 简单实现select二级联动  复制代码代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...

  2. go语言使用

    设置 package control 在 Preferences->Package Setting->Package Control->Settings - User 中加入 cha ...

  3. spring boot打成可执行jar

    第一步 添加 <packaging>jar</packaging> 第二步添加 <build> <plugins> <plugin> < ...

  4. PHP的一个小tips (关于=和==或者===的使用)

    由于我在项目中,很多场景判断等式成立的时候 都习惯把值放在==前面(例如 1 == $nStatus), 今天有个同事揪着我问为啥总这样写,回答之后今天也稍作记录下吧. 如果正常些 $nStatus ...

  5. svnkit-常用api

    0.功能列表 svnkit功能列表 1.递归获取指定目录下目录和文件,以树形展示[svn Update] 2.获取指定文件和属性(版本号.作者.日期.文件类型) 3.获取指定文件或目录的历史记录(版本 ...

  6. HALCON形状匹配讲解

    HALCON形状匹配讲解 https://blog.csdn.net/linnyn/article/details/50663328 https://blog.csdn.net/u014608071/ ...

  7. httpclient连接池在ES Restful API请求中的应用

    package com.wm.utils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http ...

  8. Acunetix WVS安全测试软件使用教程(入门级)

    1.下载 Acunetix WVS 10.5软件,下载地址为:https://pan.baidu.com/s/1Koku0Lhya5PgphMVL7w19g 密码:v438 2.压缩包中有破解说明,按 ...

  9. Django的View(视图层)

    目录 Django的View(视图层) 一.JsonResponse 二.后端接收前端的文件 三. FBV和CBV(源码分析) 四.settings.py配置文件源码分析 五. 请求对象(HttpRe ...

  10. macOS更新失败解决

    下载离线更新包解决. https://support.apple.com/downloads/combo