博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现Runnable接口方式
阅读量:5245 次
发布时间:2019-06-14

本文共 3249 字,大约阅读时间需要 10 分钟。

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

 

转载于:https://www.cnblogs.com/sunnyDream/p/7997389.html

你可能感兴趣的文章
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
vue项目中使用百度统计
查看>>
android:scaleType属性
查看>>
SuperEPC
查看>>
thymeleaf
查看>>
CentOS7安装iptables防火墙
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
python针对excel的读写操作-----openpyxl
查看>>
最后几本书,不珍藏了。
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
按钮实现A标签新窗口打开(不用window.open)
查看>>
三维变换概述
查看>>
第三次作业
查看>>