Java notify唤醒源代码的经典讲例

 

Java notify唤醒在此对象监视器上等待的单个线程。相关的问题需要我们不断的学习,下面我们就看看如何才能更好的使用。如果所有线程都在此对象上等待,则会选择唤醒其中一个线程。

  直到当前的线程放弃此对象上的锁定,才能继续执行被唤醒的线程。此方法只应由作为此对象监视器的所有者的线程来调用.

  "当前的线程必须拥有此对象监视器"与"此方法只应由作为此对象监视器的所有者的线程来调用"说明wait方法与notify方法必须在同步块内执行,即synchronized(obj之内).

  调用对像wait方法后,当前线程释放对像锁,进入等待状态.直到其他线程(也只能是其他线程)通过Java notify唤醒方法,或 notifyAll.该线程重新获得对像锁.

  继续执行,记得线程必须重新获得对像锁才能继续执行.因为synchronized代码块内没有锁是寸步不能走的.看一个很经典的例子:

  Java notify唤醒代码

  1.package ProductAndConsume;

  2.import java.util.List;

  3.public class Consume implements Runnable{

  4.private List container = null;

  5.private int count;

  6.public Consume(List lst){

  7.this.container = lst;

  8.}

  9.public void run() {

  10.while(true){

  11.synchronized (container) {

  12.if(container.size()== 0){

  13.try {

  14.container.wait();//放弃锁

  15.} catch (InterruptedException e) {

  16.e.printStackTrace();

  17.}

  18.}

  19.try {

  20.Thread.sleep(100);

  21.} catch (InterruptedException e) {

  22.// TODO Auto-generated catch block

  23.e.printStackTrace();

  24.}

  25.container.remove(0);

  26.container.notify();

  27.System.out.println("我吃了"+(++count)+"个");

  28.}

  29.}

  30.}

  31.}

  32.package ProductAndConsume;

  33.import java.util.List;

  34.public class Product implements Runnable {

  35.private List container = null;

  36.private int count;

  37.public Product(List lst) {

  38.this.container = lst;

  39.}

  40.public void run() {

  41.while (true) {

  42.synchronized (container) {

  43.if (container.size() > MultiThread.MAX) {

  44.try {

  45.container.wait();

  46.} catch (InterruptedException e) {

  47.e.printStackTrace();

  48.}

  49.}

  50.try {

  51.Thread.sleep(100);

  52.} catch (InterruptedException e) {

  53.e.printStackTrace();

  54.}

  55.container.add(new Object());

  56.container.notify();

  57.System.out.println("我生产了"+(++count)+"个");

  58.}

  59.}

  60.}

  61.}

  62.package ProductAndConsume;

  63.import java.util.ArrayList;

  64.import java.util.List;

  65.public class MultiThread {

      66.private List container = new ArrayList();

  67.public final static int MAX = 5;

  68.public static void main(String args[]){

  69.MultiThread m = new MultiThread();

  70.new Thread(new Consume(m.getContainer())).start();

  71.new Thread(new Product(m.getContainer())).start();

  72.new Thread(new Consume(m.getContainer())).start();

  73.new Thread(new Product(m.getContainer())).start();

  74.}

  75.public List getContainer() {

  76.return container;

  77.}

  78.public void setContainer(List container) {

  79.this.container = container;

  80.}

  以上就是对Java notify唤醒相关代码的介绍。希望大家有所帮助。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值