JDK1.8 (二) Thread 源码阅读笔记

  • 属性&介绍
	//这里有的未知的就不贴出来了
	private volatile char  name[];			//线程名称
    private int            priority;		//线程优先级
    private boolean     single_step;		//是否单步执行
    private boolean     stillborn = false;	//虚拟机状态
    private Runnable target;				//将会被执行目标对象
    private ThreadGroup group;				//当前线程组对象,这里会用到 SecurityManger 如
	// SecurityManager security = System.getSecurityManager(); security.getThreadGroup();
    private ClassLoader contextClassLoader; //上下文类加载器
    private static int threadInitNumber;	//线程编号,线程编号每次++实现,++不是线程安全的,
    //private static synchronized int nextThreadNum() {return threadInitNumber++;    }
    // ThreadLocal 用于实现线程内的数据共享,这里得单独讲 ThreadLocal 时候讲
    ThreadLocal.ThreadLocalMap threadLocals = null;
    ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
    private long stackSize;					//线程操作时所需堆栈大小
    private long tid;						//线程id
    //线程优先级
	public final static int MIN_PRIORITY = 1;
    public final static int NORM_PRIORITY = 5;
    public final static int MAX_PRIORITY = 10;
    //中断阻塞
    volatile Object parkBlocker;			
    private volatile Interruptible blocker;
    public static native Thread currentThread();//当前线程
    
    
  • 构造方法&介绍
	//构造方法中都调 init(...) 我们具体看
	public Thread() {
        init(null, null, "Thread-" + nextThreadNum(), 0);
    }
    //这个方法属性介绍时候说过了,编号 ++ 线程不安全用 synchronized 修饰
    private static synchronized int nextThreadNum() {
    	return threadInitNumber++;    
    }
	//init(...) 将安全检验的删掉,便于清晰观看
	private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc) {
        this.name = name.toCharArray();
        Thread parent = currentThread();
        SecurityManager security = System.getSecurityManager();
        if (g == null) {						//线程组为null 分配线程组
            if (security != null) {				//SecurityManager 分配
                g = security.getThreadGroup();
            }
            if (g == null) {					//分配在当前线程组
                g = parent.getThreadGroup();
            }
        }
        g.addUnstarted();						//安全检查
        //初始化赋值
        this.group = g;							
        this.daemon = parent.isDaemon();		
        this.priority = parent.getPriority();	
        setPriority(priority);
        this.stackSize = stackSize;
        tid = nextThreadID();
    }
  • 其它方法&介绍
 	public final synchronized void join(long millis)
    	throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;
        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {//这里主要通过isAlive() 来判断目标线程是否已经执行完来决定 join
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }
 	public State getState() {
        // 通过这里判断线程状态
        return sun.misc.VM.toThreadState(threadStatus);
    }
	//其它都是些C/C++写的方法,看不见就不介绍了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值