一个问题,下面的代码会如何运行

public class TraditionalThread {

    public static void main(String[] args) {
System.out.println("Main start " + Thread.currentThread().getName());
Thread01 thread01 = new Thread01();
thread01.tran();
}
} class Thread01 extends Thread{ @Override
public void run() {
System.out.println("thread01 " + Thread.currentThread().getName());
} public void tran() {
start();
}
}

经常使用线程的方式

面对接口编程的使用,我们一般是将线程启动的操作和线程执行的任务是分开来的,将具体任务的实现使用Runnable,所以使用线程常用的是实现Runnable接口,加上目前jdk8中常用的lambda使用起来很方便

new Thread(()->{System.out.println("Hello world")}).start()

实现线程的两种方式,继承Thread,实现Runnable

看看start方法的注释

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

翻译的意思是,调用start()方法了,java虚拟机会调用这个线程的run方法,继续看Thread的start方法里面其实会调用一个native (java虚拟机帮我们操作) 的start0方法,通过这个方法去调用的run方法。

最后一句的意思,扩展理解线程的生命周期,一个线程是不能启动两次的,会抛出非法操作异常

综上,我的理解是,不管在哪里调用Thread的start方法都会启动一个线程,调用当前类的run方法。

回到我们最开始的问题,因为在方法内部调用了start方法,表示即可启动了一个线程,所以Thread01中的run方法会执行

结果为

Main start main
thread01 Thread-0

线程基本使用--Thread内部方法调用start的更多相关文章

  1. Spring的Bean内部方法调用无法使用AOP切面(CacheAble注解失效)

    Spring的Bean内部方法调用无法使用AOP切面(CacheAble注解失效) 前言 今天在使用Spring cache的Cacheable注解的过程中遇见了一个Cacheable注解失效的问题, ...

  2. SpringBoot 内部方法调用,事务不起作用的原因及解决办法

    在做业务开发时,遇到了一个事务不起作用的问题.大概流程是这样的,方法内部的定时任务调用了一个带事务的方法,失败后事务没有回滚.查阅资料后,问题得到解决,记录下来分享给大家. 场景 我在这里模拟一个场景 ...

  3. Spring AOP无法拦截内部方法调用

    当在同一个类中,A方法调用B方法时,AOP无法工作的问题 假设一个接口里面有两个方法: package demo.long; public interface CustomerService { pu ...

  4. Spring 内部方法调用失效问题(AOP)

    AOP使用的是动态代理的机制,它会给类生成一个代理类,事务的相关操作都在代理类上完成.内部方式使用this调用方式时,使用的是实例调用,并没有通过代理类调用方法,所以会导致事务失效. 解决办法 方式一 ...

  5. AOP方法增强自身内部方法调用无效 SpringCache 例子

    开启注解@EnableCaChing,配置CacheManager,结合注解@Cacheable,@CacheEvit,@CachePut对数据进行缓存操作 缺点:内部调用,非Public方法上使用注 ...

  6. laravel PC内部方法调用

    /** * [api 内部请求] * @author Foreach * @param string $method [请求方式] * @param string $url [地址] * @param ...

  7. EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真

    EF Core使用SQL调用返回其他类型的查询   假设你想要 SQL 本身编写,而不使用 LINQ. 需要运行 SQL 查询中返回实体对象之外的内容. 在 EF Core 中,执行该操作的另一种方法 ...

  8. 注意!你的Thread.Abort方法真的让线程停止了吗?

    大家都知道在C#里面,我们可以使用 Thread.Start方法来启动一个线程,当我们想停止执行的线程时可以使用Thread.Abort方法来强制停止正在执行的线程,但是请注意,你确定调用了Threa ...

  9. C# Thread.Abort方法真的让线程停止了吗?

    大家都知道在C#里面,我们可以使用 Thread.Start方法来启动一个线程,当我们想停止执行的线程时可以使用Thread.Abort方法来强制停止正在执行的线程,但是请注意,你确定调用了Threa ...

随机推荐

  1. 跟着尚硅谷系统学习Docker-【day06】

    day06-20200720 p24.dockerfile案例编写-1   1.创建好容器时,进去容器的时候,修改所在的当前目录. 2.新添加命令.支持vim.ifconfig 备注:原来的镜像容器默 ...

  2. 20190926-01Redis五大数据类型之List 000 027

  3. 原生JDK网络编程- NIO

    什么是NIO? NIO 库是在 JDK 1.4 中引入的.NIO 弥补了原来的 I/O 的不足,它在标准 Java 代码中提供了高速的.面向块的 I/O.NIO翻译成 no-blocking io 或 ...

  4. about blog

    前言 今天无意中发现了一个小姐姐自己设计的的博客,感觉非常的nice,就随手copy一下,完了感觉效果还蛮好的 end 附上小姐姐的博客以及教程

  5. 初识ABP vNext(9):ABP模块化开发-文件管理

    Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 目录 前言 开始 创建模块 模块开发 应用服务 运行模块 单元测试 模块使用 最后 前言 在之前的章节中介绍过ABP扩展实体,当时在用户 ...

  6. 数据库漏洞扫描工具scuba

    1.先下载安装scuba 参考地址  https://www.52pojie.cn/thread-702605-1-1.html 百度网盘下载地址: 链接:https://pan.baidu.com/ ...

  7. 面向对象--继承-通过super()来调用父类方法

    super()的使用 问题: class Master(object): def __init__(self): self.kongfu = "古法煎饼果子配方" # 实例变量,属 ...

  8. python 3 for嵌套

  9. istio 常见的 10 个异常

    总结使用 istio 常见的10个异常: Service 端口命名约束 流控规则下发顺序问题 请求中断分析 sidecar 和 user container 启动顺序 Ingress Gateway ...

  10. openstack核心组件——keystone身份认证服务(5)

    云计算openstack核心组件——keystone身份认证服务(5) 部署公共环境 ntp openstack mariadb-server rabbitmq-server memcache 1.w ...