• 实现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...

    作者:凌晨六点半
    出处:http://www.cnblogs.com/sunnyDream/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。 如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击右下角的【好文要顶】按钮【精神支持】,因为这两种支持都是我继续写作,分享的最大动力!

  • 相关阅读:
    jenkins初始化启动报错导致进入web页面无法安装插件
    redis5.0.7集群搭建
    搭建redis哨兵模式
    Linux服务器安装python3.6
    MySQL绿色版安装
    OSChina中远程GIT仓库同步探索
    Android坡度计
    利用ADB获取APP资源
    实现两台路由器无线桥接
    新体能评定软件开发总结(一)
  • 原文地址:https://www.cnblogs.com/sunnyDream/p/7997389.html
Copyright © 2020-2023  润新知