您的位置:首页 >> 编程开发 >> Java >> J2SE >> 正文
RSS
 

Java之多线程(2)

http://www.rdxx.com 05年07月24日 21:14 JR 我要投稿

关键词: 多线程 , Java , 线程

    waitWhileEmpty();
    return removeAll();
  }

  public synchronized boolean waitUntilEmpty(long msTimeout) throws
      InterruptedException {
    //如果msTimeout为零,则调用waitUntilEmpty()方法
    if (msTimeout == 0L) {
      waitUntilEmpty(); // use other method
      return true;
    }

    // 等待指定的时间量
    long endTime = System.currentTimeMillis() + msTimeout;
    long msRemaining = msTimeout;

    while (!isEmpty() && (msRemaining > 0L)) {
      wait(msRemaining);
      msRemaining = endTime - System.currentTimeMillis();
    }

    // 可能超时,也可能满足了条件,计算返回值
    return isEmpty();
  }
  //此方法用于判断单元中的内容是否为空
  public synchronized void waitUntilEmpty() throws InterruptedException {

    while (!isEmpty()) {
      wait();
    }
  }

  //此方法用于判断数组中的内容是否为空,如果为空则等待
  public synchronized void waitWhileEmpty() throws InterruptedException {

    while (isEmpty()) {
      wait();
    }
  }

  public synchronized void waitUntilFull() throws InterruptedException {

    while (!isFull()) {
      wait();
    }
  }

  //此方法判断数组中的内容是否已满,如果已满的话则让其他线程等待
  public synchronized void waitWhileFull() throws InterruptedException {

    while (isFull()) {
      wait();
    }
  }
}

ObjectFIFOTest.java
public class ObjectFIFOTest
    extends Object {

  private static void fullCheck(ObjectFIFO fifo) {
    try {
      //同步化方法允许条件仍然为true时打印信息
      synchronized (fifo) {
        while (true) {
          //调用ObjectFIFO中的waitUntilFull()方法用于判断单元中的内容是否已满
          fifo.waitUntilFull();

 
 
标签: 多线程 , Java , 线程 打印本文
 
 
  相关资讯
RSS
 
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站