实现Runnable接口方式
package com.roocon.thread.t2; public class Demo2 implements Runnable {
@Override
public void run() {
while(true){
System.out.println("thread running...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
Thread thread = new Thread(new Demo2());
thread.start();
}
}
输出结果:
thread running...
thread running...
thread running...
thread running...
源码解读:
1.Thread类:根据以下代码知道,我们传入的runnable参数最后是赋值给了Thread类的属性target。
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
init(g, target, name, stackSize, null);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc) {
if (name == null) {
throw new NullPointerException("name cannot be null");
} this.name = name.toCharArray(); Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */ /* If there is a security manager, ask the security manager
what to do. */
if (security != null) {
g = security.getThreadGroup();
} /* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {
g = parent.getThreadGroup();
}
} /* checkAccess regardless of whether or not threadgroup is
explicitly passed in. */
g.checkAccess(); /*
* Do we have the required permissions?
*/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
} g.addUnstarted(); this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext =
acc != null ? acc : AccessController.getContext();
this.target = target;//赋给了Thread的属性target
setPriority(priority);
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize; /* Set thread ID */
tid = nextThreadID();
}
2.调用start方法启动线程
thread.start();
3.Thread类就去找run方法:
@Override
public void run() {
if (target != null) {
target.run();
}
}
4.于是,调用了我们runnable接口中重写的run方法。输出:
thread running...
thread running...
thread running...
thread running...
实现Runnable接口方式的更多相关文章
- 03_线程的创建和启动_实现Runnable接口方式
[线程的创建和启动的步骤(实现Runnable接口方式)] 1.定义Runnable接口的实现类,并重写其中的run方法.run()方法的方法体是线程执行体. class SonThread imp ...
- java多线程--实现Runnable接口方式
因为java类只能继承一个类可以实现多个接口的特性,所以一般情况下不推荐使用继承Thread类实现多线程,下面是实现Runnable接口方式的简单多线程代码 package text; /** * 多 ...
- Java8使用实现Runnable接口方式创建新线程的方法
环境介绍 JDK版本:1.8 开发架构:spring boot 2.x 日志:slf4j 实现步骤 Runnable接口中只有一个run()方法,它是非Thread类子类的类提供的一种激活方式.一个类 ...
- 多线程实现方式---实现Runnable接口
多线程实现方式---实现Runnable接口 一个类如果需要具备多线程的能力,也可以通过实现java.lang.Runnable接口进行实现.按照Java语言的语法,一个类可以实现任意多个接口,所以该 ...
- java基础知识回顾之java Thread类--java线程实现常见的两种方式实现Runnable接口(二)
创建线程的第二中方式: /** * 步骤: 1定义类实现Runnable接口 2.实现Runnable接口中的run方法. 3.通过Thread类建立线程对象,并将Run ...
- 创建线程的两种方式:继承Thread类和实现Runnable接口
第一种方式:继承Thread类 步骤:1.定义类继承Thread 2.覆写Threa类的run方法. 自定义代码放在run方法中,让线程运行 3.调用线程的star方法, 该线程有两个作用:启动线程, ...
- 创建线程的一般方式和匿名内部类方式对比——实现runnable接口,重新run方法
启动:使用静态代理设计模式 优点:可同时实现继承,避免单继承局限性 一般方式: Programer.java /** * 真实角色 * * @author :liuqi * @date :2018-0 ...
- 创建多线程的方式:继承Thread类和实现Runnable接口
1.通过继承Thread类的方式创建多线程(这里只是简单的代码演示创建多线程的方法) package com.baozi.exer; public class ThreadDemo { public ...
- 几种创建线程方式Thread类和Runnable接口
对于很多想学习java的人来说,经常听别人说线程难,其实真正理解了线程后,一点都不会觉得线程难,这里我为大家梳理下线程的创建方式吧. 一.线程的创建方式有三种 1.继承Thread类 2.实现Runn ...
随机推荐
- 一、openfeign的自动配置
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 openfeign是一种声明式的webservice客户端调用框架.你只需要声明接口和一 ...
- shim和polyfill 区别解释
polyfill 是 shim 的一种.shim 是将不同 api 封装成一种,比如 jQuery 的 $.ajax 封装了 XMLHttpRequest 和 IE 用 ActiveXObject 方 ...
- 【转】Java最常见的200+面试题
今天看到一份面试题总结,感觉很到位,主要包括以下模块:Java基础.容器.多线程.反射.对象拷贝.Java Web模块,异常.网络.设计模式.Spring/Spring MVC .Spring Boo ...
- 判断上传文件是否为excel
1. 可以在input上传组件上添加属性accept,这样上传文件的时候,就只能选择excel文件了. <input type="file" accept="app ...
- c语言二进制、八进制、十六进制
int binary = 0b01000010; //二进制 printf("%d\n", binary); //十进制 printf("0x%x\n", 0x ...
- Nginx 安装目录 和 编译参数
安装目录详解 查看安装nginx之后总共生成了哪些文件 rpm -ql nginx 在上面的文件中包括配置文件和日志文件 /etc/logrotate.d/nginx 类型:配置文件 作用:Nginx ...
- 说说客户端访问一个链接URL的全过程
讲讲登录权限是如何控制的 我们可以把这个过程类比成一个电话对话的过程.当我们要打电话给某个人,首先要知道对方的电话号码,然后进行拨号.打通电话后我们会进行对话,当然要对话肯定需要共同的语言,如果一 ...
- Go语言的反射
反射是语言里面是非常重要的一个特性,我们经常会看见这个词,但是对于反射没有一个很好的理解,主要是因为对于反射的使用场景不太熟悉. 一.理解变量的内在机制 1.类型信息,元信息,是预先定义好的,静态的. ...
- 持续集成实践---基于ant+jmeter+jenkins接口CI
背景: 大纲: jmeter入门教程 jenkins入门教程 ant介绍 jmeter+ant脚本自动执行实践 接口测试CI实践
- Sql Server2008——远程过程调用失败
今天正在敲机房,清理软件提醒垃圾太多并且电脑也特别卡,我就想着既然这样就清理一下得了,结果就是:No zuo No die,SQL server数据库连接不上了.不过从另一方面来说这也是一次学习的机会 ...