Get Thread By Name

前端 未结 4 1391
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 05:12

I have a multithreaded application and I assign a unique name to each thread through setName() property. Now, I want functionality to get access to the threads

4条回答
  •  执念已碎
    2020-12-08 05:30

    I like the HashMap idea best, but if you want to keep the Set, you can iterate over the Set, rather than going through the setup of converting to an array:

    Iterator i = threadSet.iterator();
    while(i.hasNext()) {
      Thread t = i.next();
      if(t.getName().equals(threadName)) return t;
    }
    return null;
    

提交回复
热议问题