线程加入不起作用

时间:2013-02-19 05:56:04

标签: java multithreading join

以下代码的逻辑是否有任何错误?尝试使用连接取决于线程是否存在。代码将无限循环。我不熟悉线程不确定逻辑是否正确,尝试引用许多我无法解决的文章。

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

2 个答案:

答案 0 :(得分:2)

join等待线程死亡。 而不是

Thread.currentThread().join();

DO

at.join();

答案 1 :(得分:0)

while (1==1) ??

这是你的问题:)