`
cii001
  • 浏览: 23427 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java线程的sleep(),wait(),notify(),yield()方法的区别要点 (zhuan)

 
阅读更多

java线程的sleep(),wait(),notify(),yield();

以前看的,很久不用忘了,现在把区别的要点重新整理一下:

1.sleep()使线程休眠一段时间,一段时间结束后,线程进入可执行状态,但并不是立即执行,只是在被排程器调用的时候才执行。在休眠期间,并不释放所持有的“锁”;

2.wait()使线程休眠一段时间,若设置参数,时间到时,线程就自动进入可执行状态。若没有,则需要notify()方法去调用。注意:wait()方法和notify()方法都时针对this对象的,调用wait()方法后,会释放加在对象上的“锁”。

3.yield()使线程放弃执行的权利,进入可执行状态,也就意味着线程在yield()方法后,有可能又执行。使用yield()方法,线程并不释放自己锁持有的“锁”。

 

看不懂。没做过线程方面的,下面的还好懂一些。

 

1、这两个方法来自不同的类分别是Thread和Object

  2、最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法。

  3、wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在

  任何地方使用(使用范围)

  synchronized(x){

  x.notify()

  //或者wait()

  }

  4、sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常

  扩充阅读:

  java 线程中的sleep和wait有一个共同作用,停止当前线程任务运行,但他们存在一定的不同,首先我们先看sleep中的构造函数

  sleep(long millis)           Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

  sleep(long millis, int nanos)          Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.

  sleep方法属于Thread类中方法,表示让一个线程进入睡眠状态,等待一定的时间之后,自动醒来进入到可运行状态,不会马上进入运行状 态,因为线程调度机制恢复线程的运行也需要时间,一个线程对象调用了sleep方法之后,并不会释放他所持有的所有对象锁,所以也就不会影响其他进程对象 的运行。但在sleep的过程中过程中有可能被其他对象调用它的interrupt(),产生InterruptedException异常,如果你的程 序不捕获这个异常,线程就会异常终止,进入TERMINATED状态,如果你的程序捕获了这个异常,那么程序就会继续执行catch语句块(可能还有 finally语句块)以及以后的代码。

  注意sleep()方法是一个静态方法,也就是说他只对当前对象有效,通过t.sleep()让t对象进入sleep,这样的做法是错误的,它只会是使当前线程被sleep 而不是t线程

  wait方法

  void wait(long timeout)

  Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

  void wait(long timeout, int nanos)

  Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

  wait属于Object的成员方法,一旦一个对象调用了wait方法,必须要采用notify()和notifyAll()方法唤醒该进程; 如果线程拥有某个或某些对象的同步锁,那么在调用了wait()后,这个线程就会释放它持有的所有同步资源,而不限于这个被调用了wait()方法的对 象。wait()方法也同样会在wait的过程中有可能被其他对象调用interrupt()方法而产生

  InterruptedException,效果以及处理方式同sleep()方法

  追加内容:

  Collection是个java.util下的接口,它是各种集合结构的父接口。

  Collections是个java.util下的类,它包含有各种有关集合操作的静态方法。

  Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。

  collections 此类完全由在 collection 上进行操作或返回 collection 的静态方法组成。它包含在 collection 上操作的多态算法,即“包装器”,包装器返回由指定 collection 支持的新 collection,以及少数其他内容。 如果为此类的方法所提供的 collection 或类对象为 null,则这些方法都会抛出 NullPointerException。



 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics