1

Is there any error in the logic of below code ? Trying to use join depending on thread is alive or not. Code is going to infinite loop. I am new to threads not sure about the logic is correct or not, tried referring many articles i couldn't solve it.

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);
        }
    }
4

2 回答 2

2

join 等待线程终止。代替

Thread.currentThread().join();

at.join();
于 2013-02-19T05:57:40.563 回答
0

while (1==1)??

这是你的问题 :)

于 2013-02-19T05:58:43.983 回答