夯实Java基础,一篇文章全解析线程问题( 二 )


  • TIMED_WAITING(超时等待)
    处于RUNNABLE状态的线程调用wait(long)方法之后,就处于等待状态,需要其他线程显示地唤醒 。
  • BLOCKED(阻塞)
    等待进入synchronized方法/代码块,处于阻塞状态 。
  • TERMINATED(终止)
    表示线程已经执行结束 。

  • 夯实Java基础,一篇文章全解析线程问题

    文章插图
    4. 线程常用方法说一下线程有哪些常用的方法 。
    方法定义含义使用方式public synchronized void start() {……}启动线程MyThread myThread = new MyThread(); myThread.start();public static native Thread currentThread();获取当前线程实例对象Thread thread = Thread.currentThread();public static native void yield();让出CPU时间片Thread.yield();public static native void sleep(long millis);睡眠指定时间Thread.sleep(1L);public void interrupt() {……}中断线程MyThread myThread = new MyThread();myThread.interrupt();public static boolean interrupted() {……}判断线程是否已中断MyThread myThread = new MyThread(); boolean interrupted = myThread.isInterrupted();public final native boolean isAlive();判断线程是否是存活状态MyThread myThread = new MyThread(); boolean alive = myThread.isAlive();public final String getName() {……}获取线程名称MyThread myThread = new MyThread(); String name = myThread.getName();public State getState() {……}获取线程状态MyThread myThread = new MyThread(); Thread.State state = myThread.getState();public long getId() {……}获取线程IDMyThread myThread = new MyThread(); long id = myThread.getId();public final void join() {……}等待其他线程执行完再执行MyThread myThread = new MyThread();myThread.join();
    我是「一灯架构」,如果本文对你有帮助,欢迎各位小伙伴点赞、评论和关注,感谢各位老铁,我们下期见

    夯实Java基础,一篇文章全解析线程问题

    文章插图

    推荐阅读