<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

        明月松間照 清泉石上流


                                            ——— 兵臨城下   貓科動物
    posts - 70, comments - 137, trackbacks - 0, articles - 23
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
    看一下下面這段程序
    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);
                }

            }

        }


    }



    運行結果為:
    java.lang.StackOverflowError
    Exception in thread "main"
    請問為什么?

    評論

    # re: 五一前最后一貼!發現一個問題,搞不明白,上來求助!  回復  更多評論   

    2008-05-01 10:28 by 隔葉黃鶯
    產生互相循環調用,看

    new Queue() 時 new 了 ThreadTest(),而 new ThreadTest() 時又 new 了 Queue() ,繼而再 new ThreadTest(),循環不已,無窮盡也

    你需要在 new Queue() 時傳遞一個已創建好的 ThreadTest 實例,如這樣的構造

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

    在 ThreadTest 構造方法中就不用處理 Queue 的創建了,或者他們的職責換一下也行。

    # re: 五一前最后一貼!發現一個問題,搞不明白,上來求助!  回復  更多評論   

    2008-05-02 15:33 by 兵臨城下
    明白了,謝了!
    主站蜘蛛池模板: 国产偷国产偷亚洲高清人| 亚洲综合小说久久另类区| 日韩精品免费一区二区三区| 麻豆国产VA免费精品高清在线| 亚洲A∨精品一区二区三区| 久久久久亚洲AV综合波多野结衣| 91天堂素人精品系列全集亚洲 | 亚洲免费一级视频| 亚洲免费二区三区| 亚洲一区免费视频| 一级毛片无遮挡免费全部| 亚洲毛片免费视频| 亚洲日韩精品一区二区三区无码 | 亚洲综合色婷婷七月丁香| 在线综合亚洲中文精品| 国内精品免费久久影院| 最近中文字幕mv免费高清在线| 免费一级成人毛片| 亚洲国产成人久久77| 久久久久国产免费| 亚洲AV无码一区二区三区国产| 亚洲人成网站色在线观看| 青青操免费在线视频| 亚洲大尺码专区影院| 国产美女视频免费观看的网站 | 老色鬼久久亚洲AV综合| 免费毛片a线观看| 亚洲一区二区无码偷拍| 亚洲尤码不卡AV麻豆| 亚洲Aⅴ无码专区在线观看q| 成人片黄网站色大片免费| 久久青草免费91线频观看站街| 久久亚洲AV成人无码国产电影 | 亚洲综合色成在线播放| 亚洲a在线视频视频| 麻豆视频免费观看| 国产亚洲福利一区二区免费看| 亚洲成av人片天堂网| 日本免费高清视频| 久久精品国产亚洲av麻豆蜜芽| 亚洲成aⅴ人片久青草影院|