gpt4 book ai didi

java - 线程连接不起作用

转载 作者:行者123 更新时间:2023-11-30 04:29:43 25 4
gpt4 key购买 nike

下面的代码逻辑有问题吗?尝试根据线程是否处于 Activity 状态来使用连接。代码将进入无限循环。我是线程新手,不确定逻辑是否正确,尝试引用很多文章但无法解决。

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

最佳答案

join 等待线程终止。而不是

Thread.currentThread().join();

at.join();

关于java - 线程连接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14950808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com