Thread.join()
将线程A加入到当前执行线程中,只有当线程A执行完毕,当前线程才能继续执行;
join方法是同步的,使用synchronized关键字,底层调用wait方法;
例:
主线程在执行过程中,调用线程对象threadA.join()方法,获得了threadA对象的锁,同时在join方法内部调用threadA的wait方法阻塞当前主线程;
Thread.ThreadLocal
InheritableThreadLocal,共享父类的ThreadLocal内容;
https://zhuanlan.zhihu.com/p/28501035
Thread.interrupt()
如果当前线程处于运行状态,调用interrupt()只是设置当前线程的中断标记位,不会中断当前线程的运行;
如果当前线程调用了wait或sleep方法,当前线程处于阻塞状态,则线程清空中断标记位,并抛出InterruptedException异常;
Thread.interrupted(),判断当前线程是否处于中断状态,如果是,返回true,同时清空中断标记位;即该方法第二次调用时将返回false;
Thread State
BLOCKED,线程阻塞在获取锁的时候
WAITING,线程调用了wait、join或LockSupport.park方法
Thread.stop() @Deprecated
调用stop方法,会释放线程持有的所有锁,此时原来被锁保护的对象可能处于中间态。如果其他线程获取了锁,并对该中间态的对象进行操作,结果无法确定;以下是官方注解。
Stopping a thread with * Thread.stop causes it to unlock all of the monitors that it * has locked (as a natural consequence of the unchecked * <code>ThreadDeath</code> exception propagating up the stack). If * any of the objects previously protected by these monitors were in * an inconsistent state, the damaged objects become visible to * other threads, potentially resulting in arbitrary behavior. Many * uses of <code>stop</code> should be replaced by code that simply * modifies some variable to indicate that the target thread should * stop running. The target thread should check this variable * regularly, and return from its run method in an orderly fashion * if the variable indicates that it is to stop running. If the * target thread waits for long periods (on a condition variable, * for example), the <code>interrupt</code> method should be used to * interrupt the wait.