Thread基础知识

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • 线程 线程(Thread)是java程序运行的基本调度单元; 在进行JUC的源码分析之前, 想回顾一下Thread...
    爱吃鱼的KK阅读 370评论 0 0
  • 问题背景,本项目借助的是前端重器,Ext JS 6.0.2的版本 这两天被一个小问题快要搞疯了。问题是这样的,前端...
    青桐人阅读 662评论 0 0
  • 最近借读了《奇特的一生》,通过当当手机阅读借阅的,时效只有三天。可以说是非常匆忙的读完了。读完之后发现,想长期有效...
    自在娇莺恰恰啼V阅读 338评论 0 1
  • 刚出生时,喜欢乱说;童年时,害羞或不敢说;少年时,叛逆(不一样)地说;青年时,想的跟说的不一样(说不清楚);现在,...
    莫说了阅读 415评论 0 2