[英]How to check if a thread is still alive in tomcat if I know the thread name?
我有一個在tomcat
運行的應用程序,並且有一個static
池( HashMap
),每次通過請求在tomcat
創建線程時,我都會在其中存儲對象,並且在線程死后從HashMap
刪除條目。 只是為了清楚起見,這是我正在做的一個簡單示例。
這是我的游泳池
public class CustomerPool{
public static Map<String, Customer> customerPool = new HashMap<String, Customer>();
public static void createSession() {
String threadName = Thread.currentThread().getName();
Customer c = new Customer();
customerPool.put(threadName, customer);
}
public static void terminateSession(){
String threadName = Thread.currentThread().getName();
customerPool.remove(threadName);
}
}
還有我的servlet
public class CustomerServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
CustomerPool.createSession();
//the rest of my code
CustomerPool.terminateSession();
return;
}
}
由於我收到一些異常(特定於我的應用程序,因此沒有指向它們的列表),我認為有時條目不會從池中刪除。
我想生成一個池的報告,該報告將顯示池中當前有多少個條目以及線程不存在於哪個條目中。 在Java中是否可以僅知道線程名稱來檢查線程是否存在?
非常感謝
您應該為此使用ThreadLocal
。 如果線程死亡,則ThreadLocal將被自動清理,因為它是Thread的類變量。
您可以枚舉JVM中的所有線程,但是如果您必須執行此操作以確保程序正常運行,它會對我有一個“氣味”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.