パブリッククラスPoolUtils { プライベートリスト<ドメイン>リスト。 公共空のinit(){ リスト=新しいのArrayList <ドメイン>(); 以下のために(INT I = 0; I <3; I ++){ list.add(新しいドメイン())。 } } パブリック同期ドメインgetDomain(){ ドメインdomain = NULL; IF(はlist.size()> 0){ ドメイン= list.remove(0)。 System.out.println(。にThread.currentThread()のgetName()+ "左ます:" +はlist.size()); }他{ {しようと するSystem.out.println(。にThread.currentThread()のgetName()+ ":前に待機を"); 待つ(); System.out.println(にThread.currentThread()のgetName()+ ":後の待ち時間"); }キャッチ(例外e){ System.out.println( "getException:" + E。 e.printStackTrace(); } ドメイン= getDomain()。 } ドメインを返します。 } 公共同期ボイドreturnDomain(ドメインドメイン){ list.add(ドメイン)。 System.out.println(にThread.currentThread()のgetName()+ "戻り値は左:"。+はlist.size()); notifyAll(); //)(通知します。 } }
パブリック静的無効メイン(文字列[] args){ 最終PoolUtils PU =新しいPoolUtils(); //单例 pu.init()。 スレッドT1 =新しいスレッド(){ ます。public void実行(){ ドメインDOM = pu.getDomain(); System.out.println(。にThread.currentThread()のgetName()+ ":GETドメイン:" + DOM)。 試す{ にThread.currentThread()睡眠(3000)。 }キャッチ(例外e){ System.out.printlnは( "getExceptionT:" + e.getMessage())。 e.printStackTrace(); } pu.returnDomain(DOM)。 System.out.println(にThread.currentThread()のgetName()+ ":リターン・ドメイン:" + DOM。); } }。 スレッドT2 =新しいスレッド(){.........