当前位置: 移动技术网 > IT编程>开发语言>Java > java多线程之wait(),notify(),notifyAll()的详解分析

java多线程之wait(),notify(),notifyAll()的详解分析

2019年07月22日  | 移动技术网IT编程  | 我要评论
wait(),notify(),notifyall()不属于thread类,而是属于object基础类,也就是说每个对象都有wait(),notify(),notifyall()的功能.因为每个对象都有锁,锁是每个对象的基础,当然操作锁的方法也是最基础了。

wait导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyall() 方法,或被其他线程中断。wait只能由持有对像锁的线程来调用。

notify唤醒在此对象监视器上等待的单个线程。如果所有线程都在此对象上等待,则会选择唤醒其中一个线程(随机)。直到当前的线程放弃此对象上的锁,才能继续执行被唤醒的线程。同wait方法一样,notify只能由持有对像锁的线程来调用.notifyall也一样,不同的是notifyall会唤配所有在此对象锁上等待的线程。
"只能由持有对像锁的线程来调用"说明wait方法与notify方法必须在同步块内执行,即synchronized(obj)之内.再者synchronized代码块内没有锁是寸步不行的,所以线程要继续执行必须获得锁。相辅相成。
看一个很经典的例子(生产者与消费者):
首先是消费者线程类:
复制代码 代码如下:

import java.util.list;
public class consume implements runnable {
 private list container = null;
 private int count;
 public consume(list lst) {
  this.container = lst;
 }
 public void run() {
  while (true) {
   synchronized (container) {
    if (container.size() == 0) {
     try {
      container.wait();// 容器为空,放弃锁,等待生产
     } catch (interruptedexception e) {
      e.printstacktrace();
     }
    }
    try {
     thread.sleep(1000);
    } catch (interruptedexception e) {
     e.printstacktrace();
    }
    container.remove(0);
    container.notify();
    system.out.println("我吃了" + (++count) + "个");
   }
  }
 }
}

接下来是生产者线程类:
复制代码 代码如下:

import java.util.list;
public class product implements runnable {
 private list container = null;
 private int count;
 public product(list lst) {
  this.container = lst;
 }
 public void run() {
  while (true) {
   synchronized (container) {
    if (container.size() > multithread.max) {
     // 如果容器超过了最大值,就不要在生产了,等待消费
     try {
      container.wait();
     } catch (interruptedexception e) {
      e.printstacktrace();
     }
    }
    try {
     thread.sleep(1000);
    } catch (interruptedexception e) {
     e.printstacktrace();
    }
    container.add(new object());
    container.notify();
    system.out.println("我生产了" + (++count) + "个");
   }
  }
 }
}

最后是测试类:
复制代码 代码如下:

import java.util.arraylist;
import java.util.list;
public class multithread {
 private list container = new arraylist();
 public final static int max = 5;
 public static void main(string args[]) {
  multithread m = new multithread();
  new thread(new consume(m.getcontainer())).start();
  new thread(new product(m.getcontainer())).start();
 }
 public list getcontainer() {
  return container;
 }
 public void setcontainer(list container) {
  this.container = container;
 }
}

运行结果如下所示:
复制代码 代码如下:

我生产了1个
我吃了1个
我生产了2个
我生产了3个
我生产了4个
我生产了5个
我生产了6个
我生产了7个
我吃了2个
我生产了8个
我吃了3个
我生产了9个
我吃了4个
我吃了5个
我吃了6个
我吃了7个
我吃了8个
我生产了10个
我生产了11个
我吃了9个
我生产了12个
我吃了10个
......

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网