李敏  
          日歷
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345
          統(tǒng)計(jì)
          • 隨筆 - 1
          • 文章 - 40
          • 評(píng)論 - 4
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿(1)

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          它山之石

          聚賢莊

          搜索

          •  

          最新評(píng)論

           

          import java.util.*;

          //工廠
          class Test{
           
            public void run(){
              Queens queen=new Queens();
             
              QueenThread qt=new QueenThread(queen);
             
              new Thread(qt).start();
             
              new Thread(qt).start();
             
              System.out.println("Len=  "+queen.getQueens().size());
            }
           
            public static void main(String[]args){
              new Test().run();
            }
          }

          //生產(chǎn)緩存
          class Queens{
            private List buffList=null;      
            private final List queenList=new ArrayList(); 
           
            public Queens(){
              init();
            }
           
            public void init(){
             int i=0;
            
             for(int len=0;len<2;len++){
                buffList=new ArrayList();
               
                for(;i<(len+1)*3;i++){
                 buffList.add(String.valueOf(i+1));
                }
                queenList.add(buffList);
             }
            }
           
            public synchronized void deleteQueen(int index){
               queenList.remove(index);
            }
           
            public List getQueens(){
              return queenList;
            }
           
            public int getLen(){
              return queenList.size();
            }
          }

          //處理隊(duì)列緩存
          class QueenThread implements Runnable{
             private static int id=0;
             private Queens queen;
            
             public QueenThread(Queens queen){
               this.queen=queen;
             }
            
             private ThreadLocal tl=new ThreadLocal(){
                protected Integer initialValue(){
                  return new Integer(id++);
                }
             };

             public void run(){
               int currentId=((Integer)(tl.get())).intValue();
              
               List queenList=(List)queen.getQueens().get(currentId);
              
               for(int i=0;i<queenList.size();i++){
                System.out.println(Thread.currentThread().getName()+" "+(String)queenList.get(i));
                
                try{
                  Thread.sleep(2000);
                }catch(InterruptedException e){
                 e.printStackTrace();
                }
               
               }
             
               System.out.println(Thread.currentThread().getName()+" ID= "+currentId);
             }
           }

          posted on 2009-07-18 17:42 李敏 閱讀(150) 評(píng)論(0)  編輯  收藏 所屬分類: 算法
           
          Copyright © 李敏 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 福海县| 聂拉木县| 麻江县| 宁国市| 镶黄旗| 扎囊县| 乌拉特后旗| 玉环县| 惠东县| 丹东市| 禹城市| 正阳县| 贵阳市| 彭阳县| 离岛区| 安泽县| 星子县| 无为县| 奉贤区| 怀柔区| 山丹县| 阳西县| 沁阳市| 吉林市| 周至县| 娄底市| 金山区| 太白县| 穆棱市| 科技| 县级市| 隆昌县| 莲花县| 辰溪县| 五大连池市| 苗栗市| 睢宁县| 怀宁县| 灌阳县| 曲水县| 中超|