美文网首页
JAVA -- 线程的sleep()方法为什么是静态的

JAVA -- 线程的sleep()方法为什么是静态的

作者: jmyang1518 | 来源:发表于2018-07-10 12:36 被阅读0次

  • sleep()只能让当前线程睡眠,当前线程是自愿的。
    • 如果让sleep()成为实例方法,当前线程可以直接sleep别的线程,会引入很多多线程的问题。
    • Thread中很多方法被弃用,destroy(), suspend(), stop(),resume()这些实例方法;剩下的只有一些static方法和只对当前线程操作方法。 为什么destroy(), suspend(), stop(),resume()这些实例方法被弃用?

  • Why is Thread.stop deprecated?

from(http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html)
Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced. Unlike other unchecked exceptions,ThreadDeath kills threads silently; thus, the user has no warning that his program may be corrupted. The corruption can manifest itself at any time after the actual damage occurs, even hours or days in the future.

©ByOtherOne

相关文章

  • JAVA线程sleep和wait方法区别

    JAVA线程sleep和wait方法区别 一 sleep 是线程类(Thread)的方法,导致此线程暂停执行指定时...

  • JAVA -- 线程的sleep()方法为什么是静态的

    sleep()只能让当前线程睡眠,当前线程是自愿的。如果让sleep()成为实例方法,当前线程可以直接sleep别...

  • 多线程-Thread常见方法

    Sleep sleep()方法是Thread类提供的静态方法,在哪个线程中调用,哪个线程阻塞,一般用于模拟网络延时...

  • 6-java多线程的基本方法

    sleep() sleep是一个静态的本地方法,主要作用是使这个线程休眠一定时间,sleep的方法声明是这样的: ...

  • Java多线程学习-2 线程主要API

    sleep 方法sleep是一个静态方法,使线程进入Runnable状态指定时间。其中sleep有一个重要的特性就...

  • Java中线程生命周期

    Java中线程生命周期图如下线程生命周期 sleep() 和 wait() 方法的区别? 1 sleep方法是Th...

  • Java线程

    1. sleep()方法(休眠)是线程类(Thread)的静态方法,调用此方法会让当前线程暂停执行指定的 ...

  • Java多线程

    Java中的sleep()和wait()的区别 sleep属于Thread类的静态方法。而wait()方法,则是属...

  • Java线程相关方法

    Java线程相关方法 1. sleep public static native void sleep(long ...

  • java线程休眠之sleep

    Sleep方法介绍 sleep方法是Thread类中的一个静态方法,当一个执行中的线程调用了Thread的slee...

网友评论

      本文标题:JAVA -- 线程的sleep()方法为什么是静态的

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