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

Java之多线程(2)

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

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

          }
        }
        Thread.sleep(1000);
      }

      while (!fifo.isEmpty()) {
        synchronized (fifo) {
          //调用remove()方法移除一项
          Object obj = fifo.remove();
          print("DATA-OUT - did remove(), obj=" + obj);
        }
        Thread.sleep(1000);
      }

      print("leaving consumer()");
    }
    catch (InterruptedException ix) {
      return;
    }
  }

  private static void producer(ObjectFIFO fifo) {
    try {
      print("just entered producer()");
      int count = 0;

      Object obj0 = new Integer(count);
      count++;
      synchronized (fifo) {
        //想数组之中放入一个对象
        fifo.add(obj0);
        print("DATA-IN - did add(), obj0=" + obj0);

        //返回数组中的内容是否为空
        boolean resultOfWait = fifo.waitUntilEmpty(500);
        print("did waitUntilEmpty(500), resultOfWait=" +
              resultOfWait + ", getSize()=" +
              fifo.getSize());
      }
      
      //向数组中放入十个内容
      for (int i = 0; i < 10; i++) {
        Object obj = new Integer(count);
        count++;
        synchronized (fifo) {
          fifo.add(obj);
          print("DATA-IN - did add(), obj=" + obj);
9 7 3 1 2 3 4 5 6 7 8 9 10 4 8 :

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



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