明月松間照 清泉石上流


                                                  ——— 兵臨城下   貓科動(dòng)物
          posts - 70, comments - 137, trackbacks - 0, articles - 23
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          看一下下面這段程序
          package com.xcblcx.test;

          import java.util.ArrayList;

          public class ThreadNotifyTest {
              
          private ArrayList list = new ArrayList();

              
          /**
               * 
          @param args
               
          */

              
          public static void main(String[] args) {
                  ThreadNotifyTest test 
          = new ThreadNotifyTest();
                  Queue queue 
          = test.new Queue();
                  
          try{
                      Thread.sleep(
          1000);
                      
          for(int i=0;i<5;i++{
                          queue.addStr(
          new Integer(i).toString());
                          Thread.sleep(
          1000);
                      }

                  }
          catch(Exception e) {
                      e.printStackTrace();
                  }

              }

              
              
          class ThreadTest extends Thread {
                  Queue queue 
          = new Queue();
                  
                  
          public ThreadTest() {
                      
          //this.queue = queue;
                  }

                  
          public void run() {
                      
          while(true{
                          
          try{
                              System.out.println(
          "Thread start .");
                              String testStr 
          = this.queue.getNext();
                              System.out.println(testStr);
                          }
          catch(Exception e ){
                              e.printStackTrace();
                          }

                      }

                  }

              }

              
              
          class Queue {
                  
          private ThreadTest threadTest = null;
                  
                  
          public Queue(){
                      
          //this.threadTest = new ThreadTest(this);
                      this.threadTest = new ThreadTest();
                      
          this.threadTest.start();
                  }

                  
                  
          public void addStr(String str) {
                      
          synchronized(list) {
                          list.add(str);
                          list.notify();
                      }

                  }

                  
                  
          public String getNext() throws InterruptedException {
                      
          synchronized(list) {
                          
          while(list.size() <= 0{
                              System.out.println(
          "wait start");
                              list.wait();
                              System.out.println(
          "wait end");
                          }

                          
                          
          return (String)list.remove(0);
                      }

                  }

              }


          }



          運(yùn)行結(jié)果為:
          java.lang.StackOverflowError
          Exception in thread "main"
          請(qǐng)問(wèn)為什么?

          評(píng)論

          # re: 五一前最后一貼!發(fā)現(xiàn)一個(gè)問(wèn)題,搞不明白,上來(lái)求助!  回復(fù)  更多評(píng)論   

          2008-05-01 10:28 by 隔葉黃鶯
          產(chǎn)生互相循環(huán)調(diào)用,看

          new Queue() 時(shí) new 了 ThreadTest(),而 new ThreadTest() 時(shí)又 new 了 Queue() ,繼而再 new ThreadTest(),循環(huán)不已,無(wú)窮盡也

          你需要在 new Queue() 時(shí)傳遞一個(gè)已創(chuàng)建好的 ThreadTest 實(shí)例,如這樣的構(gòu)造

          public Queue(ThreadTest threadTest){
          this.threadTest = threadTest;
          }

          在 ThreadTest 構(gòu)造方法中就不用處理 Queue 的創(chuàng)建了,或者他們的職責(zé)換一下也行。

          # re: 五一前最后一貼!發(fā)現(xiàn)一個(gè)問(wèn)題,搞不明白,上來(lái)求助!  回復(fù)  更多評(píng)論   

          2008-05-02 15:33 by 兵臨城下
          明白了,謝了!
          主站蜘蛛池模板: 光泽县| 古蔺县| 苏尼特右旗| 万全县| 乐业县| 且末县| 临洮县| 库车县| 丰原市| 和龙市| 依安县| 岳阳市| 莱西市| 北票市| 阜康市| 莱芜市| 昂仁县| 寻乌县| 台江县| 孙吴县| 临漳县| 岳阳县| 孟连| 山东省| 芮城县| 沂水县| 正宁县| 满城县| 化德县| 伊春市| 新乡县| 万载县| 大新县| 怀化市| 潼南县| 来安县| 南丰县| 剑川县| 达孜县| 芜湖市| 临汾市|