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...

最新文章

  1. [Hadoop大数据]——Hive部署入门教程
  2. thinkPHP3.2.3集成swoole扩展
  3. (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息
  4. Listview的点击事件
  5. Debug with jdb
  6. UML视图(九)部署图
  7. 将C# dataTable 做为参数传入到存储过程
  8. 关于tcc、tlink的编译链接机制的研究
  9. IMG图片垂直居中的问题
  10. [Asp.Net Core轻量级Aop解决方案]AspectCore Project 介绍
  11. Java 9 揭秘(3. 创建你的第一个模块)
  12. Hibernate第八篇【懒加载】
  13. LINQ之道
  14. sea.js简单使用教程
  15. java的优点和误解 《java核心技术卷i》第一章
  16. $Django setting.py配置 ,GET、POST深入理解,三件套,orm对象关系映射简介
  17. 上传文件异常 MultipartException
  18. Java基础-Eclipse第三方安装包管理工具之Maven
  19. HDU1875+Prim模板
  20. Daily Scrum1 11.3

热门文章

  1. 【转载】C#使用Except方法求取两个List集合的差集数据
  2. Java 之 Junit 单元测试
  3. 【charlse】charlse功能
  4. js rsa sign使用笔记(加密,解密,签名,验签)
  5. Docker那些事儿之编排工具docker-compose
  6. Codeforces Round #609 (Div. 2) D. Domino for Young
  7. Java8新特性--Optional
  8. vue2 运动及相关函数
  9. Django REST framework+Vue 打造生鲜电商项目(笔记三)
  10. 61、springmvc-异步请求-返回DeferredResult