实现Runnable接口方式
2024-08-27 10:01:52
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...
最新文章
- [Hadoop大数据]——Hive部署入门教程
- thinkPHP3.2.3集成swoole扩展
- (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息
- Listview的点击事件
- Debug with jdb
- UML视图(九)部署图
- 将C# dataTable 做为参数传入到存储过程
- 关于tcc、tlink的编译链接机制的研究
- IMG图片垂直居中的问题
- [Asp.Net Core轻量级Aop解决方案]AspectCore Project 介绍
- Java 9 揭秘(3. 创建你的第一个模块)
- Hibernate第八篇【懒加载】
- LINQ之道
- sea.js简单使用教程
- java的优点和误解 《java核心技术卷i》第一章
- $Django setting.py配置 ,GET、POST深入理解,三件套,orm对象关系映射简介
- 上传文件异常 MultipartException
- Java基础-Eclipse第三方安装包管理工具之Maven
- HDU1875+Prim模板
- Daily Scrum1 11.3
热门文章
- 【转载】C#使用Except方法求取两个List集合的差集数据
- Java 之 Junit 单元测试
- 【charlse】charlse功能
- js rsa sign使用笔记(加密,解密,签名,验签)
- Docker那些事儿之编排工具docker-compose
- Codeforces Round #609 (Div. 2) D. Domino for Young
- Java8新特性--Optional
- vue2 运动及相关函数
- Django REST framework+Vue 打造生鲜电商项目(笔记三)
- 61、springmvc-异步请求-返回DeferredResult