Java.lang.Object类中应用在多线程的几种方法介绍

    Java.lang.Object类是所有类的基类,它里面的方法都是非常重要的,每一个方法我们都要知道它的用法,现在我们主要来介绍它们中应用在多线程的几种方法。

1. wait()

public final void wait() throws InterruptedException

  Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). 

  The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution. 

  As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop: 

     synchronized (obj) {

         while (<condition does not hold>)

             obj.wait();

         ... // Perform action appropriate to condition

     }

  This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor. 

翻译: 

  造成当前线程的等待,至到另外一个线程的调用了当前这个对象的notify()方法或者notifyAll()方法(它才继续执行),换句话说,这个方法它的行为非常类似于它简单的执行调用了wait(0).(其实是wait()方法底层调用了wait(0)而已)

  当前的线程必须要有这个对象的监视器(锁)(只有当前线程在synchronized块,或者在synchronized方法中才能保证当前这个线程拥有所在对象的锁,换句话说我们调用wait()一定是在synchronized块,或者在synchronized方法里面),这个线程会释放掉锁的拥有权然后等待至到另外一个线程通知正在等待这个对象锁的那些线程唤醒它们通过调用notify()或者notifyAll()方法。这个线程然后就等待至到它重新获得锁的拥有权然后继续执行。

  在只有一个参数的版本中,中断和虚假唤醒都是存在,这个方法总是在循环中使用,如下所示:

synchronized (obj) {

         while (<condition does not hold>)

             obj.wait();

         ... // Perform action appropriate to condition

     }

[整体逻辑是这样的:线程进入到synchronizd代码块,获得到锁,执行了wait()方法之后,就会释放掉锁并且等待进入睡眠,至到另外获得到锁的那个线程调用了notify()方法和notifyAll()方法之后,它就会被唤醒,然后等待至到它重新得到对象锁,然后继续执行wait()方法之后的代码。]

  这个方法只能只能被对象锁的拥有者的线程所调用(再次强调wait()只能在synchronized块里面被调用),对于一个线程能够成为监视器的拥有者的描述请参考notify()方法。


2. notify()

public final void notify()
  Wakes up a single thread that is waiting on this object's monitor. 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 wait methods. 

  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.

  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:

  •  By executing a synchronized instance method of that object.
  •  By executing the body of a synchronized statement that synchronizes on the object.
  •  For objects of type Class, by executing a synchronized static method of that class.

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


翻译:  

  唤醒正在等待对象锁的一个单线程,如果有任意的线程都在等待这个对象,会选择他们中的一个被唤醒的,这种选择是武断的并且会导致有实现去决定的(假如多个线程都在等待被调用唤醒,具体唤醒哪个是任意的,是随机的),一个线程等待对象锁通过调用wait()方法重载版本中的一个。

  被唤醒的线程是不能马上执行的至到当前线程放弃掉它的对象锁。被唤醒的线程会用通常的方法与其他有可能竞争对象同步块的线程竞争,比如说,被唤醒的线程没有什么特权或者缺点(有可能与线程的优先级有关系)成为下一个获得对象锁的线程。

  这个方法只应该被拥有对象锁的线程去调用(也就是说notify()也是在synchronized块或者synchronized方法中被调用,与wait()方法一样),一个线程会成为对象锁的拥有者可以通过一下三种方式:

  •  通过执行那个object对象的synchronized的实例方法 [相当于synchronized(object)]
  •  通过执行一个与synchronizes这个对象进行同步的synchronized语句 [相当于synchronized(this)]
  •  对于一个class对象,执行一个这个类的 static synchronized 方法 
  同一个时间有且仅有一个线程能够拥有对象锁。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值