執行緒快取獲取執行緒,進行執行緒銷燬操作
阿新 • • 發佈:2019-01-08
實現類
@Override public boolean stopNetData(NetworkSystem networkSystem) { String mqthread = networkSystem.getMqthread(); Thread s = findThread(Long.parseLong(mqthread)); if (s!=null){ MqttClient mqclient = (MqttClient)CacheUtil.getCache(String.valueOf(s.getId())); if (mqclient!=null){ try { mqclient.disconnect(); mqclient.close(); CacheUtil.rmCache(String.valueOf(s.getId())); } catch (MqttException e) { e.printStackTrace(); } } try { s.interrupt(); s.interrupt(); System.out.println("停止執行緒正確處理"); networkSystem.setMqstatus(2); this.updateAllColumnById(networkSystem); }catch (Exception e){ System.out.println("停止異常處理"); } return true; }else{ System.out.println("查詢無執行緒"); return true; } }
執行緒快取工具類
public class CacheUtil { public static final HashMap<String, Object> cacheMap = new HashMap<String, Object>(); public static Object getCache(String keyValue) { Object value = null; value = cacheMap.get(keyValue); if (value == null) { return null; } return value; } public static boolean putCache(String keyValue, Object ThreadName) { Object put = cacheMap.put(keyValue, ThreadName); if (put != null) { return true; } return false; } public static boolean rmCache(String keyValue) { Object remove = cacheMap.remove(keyValue); if (remove != null) { return true; } return false; } }