美文网首页
Thread基础知识

Thread基础知识

作者: 逍遥游的境界 | 来源:发表于2018-03-31 16:37 被阅读0次

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.

相关文章

  • 多线程

    _thread模块 基础知识 开启一个新线程 _thread.start_new_thread(functio...

  • Thread基础知识

    Thread.join() 将线程A加入到当前执行线程中,只有当线程A执行完毕,当前线程才能继续执行; join方...

  • Thread 基础知识

    线程 线程(Thread)是java程序运行的基本调度单元; 在进行JUC的源码分析之前, 想回顾一下Thread...

  • Python爬虫速度很慢?并发编程了解一下吧

    文章目录 前言 基础知识 GIL 多线程 创建Thread 对象 自定义类继承 Thread 私信小编01即可获取...

  • android面试/笔试题归纳2

    第二波,继续继续,大家多多指教。 题目 自定义view service的基础知识 thread在appliciti...

  • Java并发整理

    读Java并发专题总结 一. 基础知识 新建线程继承Thread类,重写run方法实现Runable接口实现Cal...

  • (Java) 查看线程状态

    参考资料 (豆瓣链接)Java核心技术 卷I 基础知识 第10版 英文版 的 14.3 Thread States...

  • Java基础知识复盘-多线程

    一、Java基础知识复盘-多线程 1.1.线程的创建和使用 方式一:继承Thread类 创建一个类继承于Threa...

  • python多线程之二——threading模块

    上一篇文章讲了python多线程的基础知识和thread模块,这一篇着重讲解一下threading模块 threa...

  • Android 计时的两种思路

    1.Android中计时 趁最近两周不忙,自己支配的时间比较多,正好查漏补缺,这两天看了些Thread的基础知识,...

网友评论

      本文标题:Thread基础知识

      本文链接:https://www.haomeiwen.com/subject/lmagcftx.html