6_关于wait, notify与notifyAll

  • wait

    (1) wait是Object类的非静态方法,也就是说,所有对象实例都有wait方法

    (2) Object类中提供了wait的__三种__重载形式

      1° public final void wait() throws InterruptedException;
    
      2° public final native void wait(long timeout) throws InterruptedException;
    
      3° public final void wait(long timeout, int nanos) throws InterruptedException
    

    它们的根源是第2°种形式

    (3) 源码中的函数说明信息

    1° wait的作用是让调用它的当前线程等待,直到 被notify被interrupt等待时间到达设定的时间

    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

    2° 当前线程必须拥有调用对象的锁

    The current thread must own this object’s monitor lock.

    3° wait的作用是 1.将当前线程放入__等待池__ 2.释放当前对象上的锁

    This method causes the current thread to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object.

    但是要注意,释放的只是调用的对象上的锁,其他对象的锁并不释放

    Note that only the locks on this object are relinquished; any other objects on which the current thread may be synchronized remain locked while the thread waits.

    4° Thread then becomes disabled for thread scheduling purposes and lies dormant until one of the following occurs:

      1. Some other thread invokes the {@code notify} method for this object and thread happens to be arbitrarily chosen as the thread to be awakened.
    
      2. Some other thread invokes the {@code notifyAll} method for this object.
    
      3. Some other thread {@linkplain Thread#interrupt() interrupts} thread 
    
      4. The specified amount of real time has elapsed, more or less.
      
      5. Thread T is awakened spuriously
    

    5° 出现了4°中的几种情况后,当前线程会移出等待池,然后像正常的情况一样和其他线程竞争。如果得到了调度,那么当前对象上的同步信息会恢复到使用 object.wait()之前的情况

    The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It competes in the usual manner with other threads for the right to synchronize on the object; once it has regained control of the object, all its synchronization claims on the object are restored to the status quo-ante – that is, to the situation as of the time that the {@code wait} method was invoked. Thread T then returns from the invocation of the {@code wait} method. Thus, on return from the {@code wait} method, the synchronization state of the object and of thread {@code T} is exactly as it was when the {@code wait} method was invoked.

  • notify

    (1) Object类中的非静态方法

      public final native void notify();
    

    (2) 源码中的函数说明

    1° 会唤醒__一个__等待这个对象锁的线程

    Wakes up a single thread that is waiting on this object’s monitor.

    2° 如果有多个等待该对象锁的线程,会选择一个唤醒。这种选择是__随意的__。

    If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object’s monitor by calling one of the {@code wait} methods.

    3° 被唤醒后的线程没有特权或劣势,它只不过是一个普通线程重新去竞争线程调度

    The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

    4° 只有一个线程是某个锁的所有者时,才能调用这个notify()方法。有__三种__可以获得锁的方法

    This method should only be called by a thread that is the owner of this object’s monitor. A thread becomes the owner of the object’s monitor in one of three ways:

      1. By executing a synchronized instance method of that object.
    
      2. By executing the body of a {@code synchronized} statement that synchronizes on the object.
    
      3. For objects of type {@code Class} by executing a synchronized static method of that class.
    

    Only one thread at a time can own an object’s monitor.

  • notifyAll

    (1) Object类中的非静态方法

      public final native void notifyAll();
    

    (2) 源码中的函数说明

    1° 唤醒所有等待这个对象锁的线程

    Wakes up all threads that are waiting on this object’s monitor. A thread waits on an object’s monitor by calling one of the {@code wait} methods.

    2° 被唤醒后的所有线程没有特权或劣势,它们都是普通线程重新去竞争线程调度

    The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

    5° 只有一个线程是某个锁的所有者时,才能调用这个notify()方法。有__三种__可以获得锁的方法

    This method should only be called by a thread that is the owner of this object’s monitor. A thread becomes the owner of the object’s monitor in one of three ways:

      1. By executing a synchronized instance method of that object.
    
      2. By executing the body of a {@code synchronized} statement that synchronizes on the object.
    
      3. For objects of type {@code Class} by executing a synchronized static method of that class.
    

    Only one thread at a time can own an object’s monitor.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值