2013-02-19 53 views
1

下面的代碼邏輯中是否有任何錯誤?試圖根據線程使用連接是否存在。代碼將無限循環。我是線程新手,不確定邏輯是否正確,嘗試引用很多我無法解決的文章。線程加入不工作

protected static HashMap<Integer, Object> MatchMap = new HashMap<Integer, Object>(); 
synchronized(this) 
    { 
     //HashMap<String, Object> MatchMap = new HashMap<String, Object>(); 
     String this_object = ""; 
     Thread at ; 
     int cstd_tsk= 0; 
     cstd_tsk = this.CSTDN_ID; 

     this_object = this.SCA_REF + ":" + this.INSTRMNT_ID + ":" + this.CSTDN_ID + ":" + this.TXN_TYPE; 

     log.error("this_object>>>"+ this_object); 
     try{ 
     while (1==1) 
     //for(int n=0; n<100; n++) 
     {        
      if(MatchMap.containsKey(cstd_tsk)) 
      { 
      /* Thread.currentThread().join(); 
       MatchMap.remove(this_object); 
       MatchMap.put(this_object, (Object)Thread.currentThread()); 
      */ 
       at = (Thread)MatchMap.get(cstd_tsk); 
       if(at.isAlive()) 
       { 
       Thread.currentThread().join(); 
       log.error("tsk is_alive>>>"+at.getId()); 
       } 
       else 
       { 
        MatchMap.remove(cstd_tsk); 
        MatchMap.put(new Integer(this.CSTDN_ID), (Object)Thread.currentThread()); 
        log.error("tsk not_alive>>>"); 
        break; 
       }        
      } 
      else 
      { 
       MatchMap.put(new Integer(this.CSTDN_ID), (Object)Thread.currentThread()); 
       log.error("tsk does not contain>>>"+Thread.currentThread().getId()); 
       break; 
      } 
     } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
      log.error("Stack trace is - " + e); 
      //System.out.println(e); 
     } 
    } 
+0

確保正確處理'InterruptedException' – bowmore 2013-02-19 06:16:04

+0

請添加代碼,其他人可以輕鬆地編譯和執行在他們的機器。閱讀http://sscce.org/ – Ankit 2013-02-19 06:54:52

回答

2

加入了這個線程等待死亡。 而不是

Thread.currentThread().join(); 

at.join(); 
0

while (1==1)

這是你的問題:)