之前的文章我介紹了C#版本的多線程和自定義線程處理器。
接下來我們來看看Java版本的呢
java 的線程和C#的線程有點區別,java的線程沒有是否是后台線程一說,具體原因是java的線程是jvm的c++代碼模擬線程,而C#的線程也是C++模擬線程。但是區別在於C#的線程會基於系統的線程。
C# 的 Thread.IsBackground;
這里唯一的區別在於,C#開啟線程如果是非后台線程即便是你關閉了程序,如果不是強制退出進程的情況下。線程還會繼續運行,知道垃圾回收機制強制回收。如果設置了后台線程標識,關閉程序就直接退出。
java沒有一說。
java線程有分組和底層線程ID一說。C#沒有。
java的線程可以自定義線程運行接口 Runnable 或者 重寫線程run()方法。
其實這些都是大同小異的,區別性不大。
java線程的基礎知識,到處都是,我不在BB
直接開始整體吧~!
1 /** 2 * 線程模型 3 * 4 * @author 失足程序員 5 * @Blog http://www.cnblogs.com/ty408/ 6 * @mail 492794628@qq.com 7 * @phone 13882122019 8 * 9 */ 10 public class ThreadModel extends Thread { 11 12 private static final Logger log = Logger.getLogger(TaskModel.class); 13 private static int threadID = 0; 14 private static final Object SYN_OBJECT = new Object(); 15 private long tid; 16 /** 17 * 任務列表 線程安全的任務列表 18 */ 19 protected final List<TaskModel> taskQueue = Collections.synchronizedList(new LinkedList<TaskModel>()); 20 //false標識刪除線程 21 private boolean runing = true; 22 23 public ThreadModel(ThreadGroup group) { 24 this(group, "無名"); 25 } 26 27 public ThreadModel(ThreadGroup group, String name) { 28 super(group, name); 29 synchronized (SYN_OBJECT) { 30 threadID++; 31 tid = threadID; 32 } 33 } 34 35 @Override 36 public long getId() { 37 return this.tid; 38 } 39 40 /** 41 * 增加新的任務 每增加一個新任務,都要喚醒任務隊列 42 * 43 * @param runnable 44 */ 45 public void addTask(TaskModel runnable) { 46 synchronized (taskQueue) { 47 taskQueue.add(runnable); 48 /* 喚醒隊列, 開始執行 */ 49 taskQueue.notify(); 50 } 51 } 52 53 public void setRuning(boolean runing) { 54 this.runing = runing; 55 } 56 57 @Override 58 public void run() { 59 while (runing && ThreadManager.getInstance().isRunning()) { 60 TaskModel r = null; 61 while (taskQueue.isEmpty() && runing && ThreadManager.getInstance().isRunning()) { 62 try { 63 /* 任務隊列為空,則等待有新任務加入從而被喚醒 */ 64 synchronized (taskQueue) { 65 taskQueue.wait(500); 66 } 67 } catch (InterruptedException ie) { 68 log.error(ie); 69 } 70 } 71 synchronized (taskQueue) { 72 /* 取出任務執行 */ 73 if (runing && ThreadManager.getInstance().isRunning()) { 74 r = taskQueue.remove(0); 75 } 76 } 77 if (r != null) { 78 /* 執行任務 */ 79 //r.setSubmitTimeL(); 80 long submitTime = System.currentTimeMillis(); 81 try { 82 r.run(); 83 } catch (Exception e) { 84 log.error("工人<“" + Thread.currentThread().getName() + "”> 執行任務<" + r.getID() + "(“" + r.getName() + "”)> 遇到錯誤: " + e); 85 e.printStackTrace(); 86 } 87 long timeL1 = System.currentTimeMillis() - submitTime; 88 long timeL2 = System.currentTimeMillis() - r.getSubmitTime(); 89 if (timeL1 <= 100L) { 90 log.info("工人<“" + Thread.currentThread().getName() + "”> 完成了任務:" + r.toString() + " 執行耗時:" + timeL1 + " 提交耗時:" + timeL2); 91 } else if (timeL1 <= 1000L) { 92 log.info("工人<“" + Thread.currentThread().getName() + "”> 長時間執行 完成任務:" + r.toString() + " “考慮”任務腳本邏輯 耗時:" + timeL1 + " 提交耗時:" + timeL2); 93 } else if (timeL1 <= 4000L) { 94 log.info("工人<“" + Thread.currentThread().getName() + "”> 超長時間執行完成 任務:" + r.toString() + " “檢查”任務腳本邏輯 耗時:" + timeL1 + " 提交耗時:" + timeL2); 95 } else { 96 log.info("工人<“" + Thread.currentThread().getName() + "”> 超長時間執行完成 任務:" + r.toString() + " “考慮是否應該刪除”任務腳本 耗時:" + timeL1 + " 提交耗時:" + timeL2); 97 } 98 r = null; 99 } 100 } 101 log.error("線程結束, 工人<“" + Thread.currentThread().getName() + "”>退出"); 102 } 103 104 @Override 105 public String toString() { 106 return "Thread{" + "tid=" + tid + ",Name=" + this.getName() + '}'; 107 } 108 109 }
這里創建我們自定義的線程模型,有兩點值得注意的是,
protected final List<TaskModel> taskQueue = Collections.synchronizedList(new LinkedList<TaskModel>());
synchronized (taskQueue) { taskQueue.add(runnable); /* 喚醒隊列, 開始執行 */ taskQueue.notify(); } synchronized (taskQueue) { taskQueue.wait(500); }
這里我們同樣沒有使用Thread.Sleep();對線程進行暫停,同樣使用的是 taskQueue.wait();來進行線程暫停,這是因為當任務隊列為空的時候,需要暫停線程。
當新的任務被放進來的時候,又必須立即開始執行任務。
為了防止線程永久暫停,設置的是500毫秒,這樣我們需要關閉程序(ThreadManager.getInstance().isRunning()==false)停止線程時候他會自定停止。
輔助鍵值對存儲器

1 /** 2 * 輔助鍵值對存儲 3 * 4 * @author 失足程序員 5 * @Blog http://www.cnblogs.com/ty408/ 6 * @mail 492794628@qq.com 7 * @phone 13882122019 8 */ 9 public class ObjectAttribute extends HashMap<String, Object> { 10 11 private static final long serialVersionUID = -5320260807959251398L; 12 13 /** 14 * 調用此方法 刪除值是需要保證存在key值和value值 否則空指針報錯 15 * 16 * @param <T> 17 * @param key 18 * @param clazz 19 * @return 20 * @deprecated 需要保證存在key值和value值 否則空指針報錯 慎重 21 */ 22 @Deprecated 23 public <T extends Object> T remove(String key, Class<T> clazz) { 24 Object obj = this.remove(key); 25 return (T) obj; 26 } 27 28 /** 29 * 如果未找到也返回 null 30 * 31 * @param key 32 * @return 33 */ 34 public String getStringValue(String key) { 35 if (this.containsKey(key)) { 36 return this.get(key).toString(); 37 } 38 return null; 39 } 40 41 /** 42 * 如果未找到也返回 0 43 * 44 * @param key 45 * @return 46 */ 47 public int getintValue(String key) { 48 if (this.containsKey(key)) { 49 return (int) (this.get(key)); 50 } 51 return 0; 52 } 53 54 /** 55 * 如果未找到也返回 null 56 * 57 * @param key 58 * @return 59 */ 60 public Integer getIntegerValue(String key) { 61 if (this.containsKey(key)) { 62 return (Integer) (this.get(key)); 63 } 64 return null; 65 } 66 67 /** 68 * 如果未找到也返回 0 69 * 70 * @param key 71 * @return 72 */ 73 public long getlongValue(String key) { 74 if (this.containsKey(key)) { 75 return (long) (this.get(key)); 76 } 77 return 0; 78 } 79 80 /** 81 * 如果未找到也返回 null 82 * 83 * @param key 84 * @return 85 */ 86 public Long getLongValue(String key) { 87 if (this.containsKey(key)) { 88 return (Long) (this.get(key)); 89 } 90 return null; 91 } 92 93 /** 94 * 如果未找到也返回 0 95 * 96 * @param key 97 * @return 98 */ 99 public float getfloatValue(String key) { 100 if (this.containsKey(key)) { 101 return (float) (this.get(key)); 102 } 103 return 0; 104 } 105 106 /** 107 * 如果未找到也返回 null 108 * 109 * @param key 110 * @return 111 */ 112 public Float getFloatValue(String key) { 113 if (this.containsKey(key)) { 114 return (Float) (this.get(key)); 115 } 116 return null; 117 } 118 119 /** 120 * 如果未找到也返回 false 121 * 122 * @param key 123 * @return 124 */ 125 public boolean getbooleanValue(String key) { 126 if (this.containsKey(key)) { 127 return (boolean) (this.get(key)); 128 } 129 return false; 130 } 131 132 /** 133 * 如果未找到也返回 null 134 * 135 * @param key 136 * @return 137 */ 138 public Boolean getBooleanValue(String key) { 139 if (this.containsKey(key)) { 140 return (Boolean) (this.get(key)); 141 } 142 return null; 143 } 144 145 @Override 146 public Object clone() { 147 return super.clone(); //To change body of generated methods, choose Tools | Templates. 148 } 149 }
任務執行模型
1 /** 2 * 任務模型 3 * 4 * @author 失足程序員 5 * @Blog http://www.cnblogs.com/ty408/ 6 * @mail 492794628@qq.com 7 * @phone 13882122019 8 * 9 */ 10 public abstract class TaskModel { 11 12 private static final Logger log = Logger.getLogger(TaskModel.class); 13 14 private long ID; 15 private String Name; 16 //運行時數據 17 private ObjectAttribute runAttribute = new ObjectAttribute(); 18 19 public TaskModel(long ID, String Name) { 20 this.ID = ID; 21 this.Name = Name; 22 this.runAttribute.put("submitTime", System.currentTimeMillis()); 23 } 24 25 public TaskModel() { 26 this(0, "無名"); 27 } 28 29 public long getSubmitTime() { 30 return this.runAttribute.getlongValue("submitTime"); 31 } 32 33 public ObjectAttribute getRunAttribute() { 34 return runAttribute; 35 } 36 37 public void setRunAttribute(ObjectAttribute runAttribute) { 38 this.runAttribute = runAttribute; 39 } 40 41 public long getID() { 42 return ID; 43 } 44 45 public String getName() { 46 return Name; 47 } 48 49 public abstract void run(); 50 51 @Override 52 public String toString() { 53 return "TaskModel{" + "ID=" + ID + ", Name=" + Name + ", runAttribute=" + runAttribute + '}'; 54 } 55 56 }
接下來我們測試一下
1 ThreadModel threadModel = new ThreadModel(new ThreadGroup("Test"), "Test"); 2 threadModel.start(); 3 threadModel.addTask(new TaskModel() { 4 5 @Override 6 public void run() { 7 System.out.println("TaskModel Test"); 8 } 9 });
執行結果
TaskModel Test
[04-24 17:31:26:0223:INFO : sz.network.threadpool.TaskModel:97 行] -> 工人<“Test”> 完成了任務:TaskModel{ID=0, Name=無名, runAttribute={submitTime=1429867886219}} 執行耗時:0 提交耗時:4
我們看到居然有提交耗時,,別奇怪,,因為在某些情況下,線程的從暫停狀態到喚醒狀態需要消耗時間的,系統不可能有那么多空閑資源,收到你的命令馬上就放棄一切事情執行你的命令。
我們調試運行時可以看見當前程序所有線程,以及分組情況(我使用的是NetBeans IDE 8.0.2 開發工具)
接下來我們來構建一下。后台線程池,
1 /** 2 * 后台線程池 3 * 4 * @author 失足程序員 5 * @Blog http://www.cnblogs.com/ty408/ 6 * @mail 492794628@qq.com 7 * @phone 13882122019 8 */ 9 class BackThread { 10 11 private static final Logger log = Logger.getLogger(BackThread.class); 12 13 private final ThreadGroup threadGroup = new ThreadGroup(ThreadManager.getGlobeThreadGroup(), "后台執行器"); 14 15 /* 任務列表 */ 16 private final List<TaskModel> taskQueue = Collections.synchronizedList(new LinkedList<TaskModel>()); 17 private final BackThreadRunnable backThreadRunnable = new BackThreadRunnable(); 18 19 public BackThread() { 20 int threadcountI = 10; 21 for (int i = 1; i <= threadcountI; i++) { 22 Thread thread = new Thread(threadGroup, backThreadRunnable, "后台線程-" + i); 23 thread.start(); 24 } 25 log.info("---初始化后台線程池--線程數量:" + threadcountI + "------------"); 26 } 27 28 /** 29 * 增加新的任務 每增加一個新任務,都要喚醒任務隊列 30 * 31 * @param newTask 32 */ 33 public void addTask(TaskModel newTask) { 34 synchronized (taskQueue) { 35 taskQueue.add(newTask); 36 /* 喚醒隊列, 開始執行 */ 37 taskQueue.notify(); 38 } 39 } 40 41 final class BackThreadRunnable implements Runnable { 42 43 /** 44 * 循環執行任務 45 */ 46 @Override 47 public void run() { 48 while (ThreadManager.getInstance().isRunning()) { 49 TaskModel r = null; 50 synchronized (taskQueue) { 51 while (taskQueue.isEmpty() && ThreadManager.getInstance().isRunning()) { 52 try { 53 /* 任務隊列為空,則等待有新任務加入從而被喚醒 */ 54 taskQueue.wait(500); 55 } catch (InterruptedException ie) { 56 log.error(ie); 57 } 58 } 59 /* 取出任務執行 */ 60 if (ThreadManager.getInstance().isRunning()) { 61 r = taskQueue.remove(0); 62 } 63 } 64 if (r != null) { 65 /* 執行任務 */ 66 //r.setSubmitTimeL(); 67 long submitTime = System.currentTimeMillis(); 68 try { 69 r.run(); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 log.error("工人<“" + Thread.currentThread().getName() + "”> 執行任務<" + r.getID() + "(“" + r.getName() + "”)> 遇到錯誤: " + e); 73 } 74 long timeL1 = System.currentTimeMillis() - submitTime; 75 long timeL2 = System.currentTimeMillis() - r.getSubmitTime(); 76 if (timeL1 <= 100L) { 77 log.info("工人<“" + Thread.currentThread().getName() + "”> 完成了任務:" + r.toString() + " 執行耗時:" + timeL1 + " 提交耗時:" + timeL2); 78 } else if (timeL1 <= 1000L) { 79 log.info("工人<“" + Thread.currentThread().getName() + "”> 長時間執行 完成任務:" + r.toString() + " “考慮”任務腳本邏輯 耗時:" + timeL1 + " 提交耗時:" + timeL2); 80 } else if (timeL1 <= 4000L) { 81 log.info("工人<“" + Thread.currentThread().getName() + "”> 超長時間執行完成 任務:" + r.toString() + " “檢查”任務腳本邏輯 耗時:" + timeL1 + " 提交耗時:" + timeL2); 82 } else { 83 log.info("工人<“" + Thread.currentThread().getName() + "”> 超長時間執行完成 任務:" + r.toString() + " “考慮是否應該刪除”任務腳本 耗時:" + timeL1 + " 提交耗時:" + timeL2); 84 } 85 r = null; 86 } 87 } 88 log.error("線程結束, 工人<“" + Thread.currentThread().getName() + "”>退出"); 89 } 90 } 91 }
以及定時器線程處理器
1 /** 2 * 定時器線程 3 * 4 * @author 失足程序員 5 * @Blog http://www.cnblogs.com/ty408/ 6 * @mail 492794628@qq.com 7 * @phone 13882122019 8 */ 9 class TimerThread extends ThreadModel { 10 11 private static final Logger log = Logger.getLogger(TimerThread.class); 12 13 public TimerThread() { 14 super(ThreadManager.getGlobeThreadGroup(), "全局定時器線程"); 15 this.start(); 16 } 17 18 @Override 19 public void run() { 20 while (ThreadManager.getInstance().isRunning()) { 21 while (ThreadManager.getInstance().isRunning() && taskQueue.isEmpty()) { 22 try { 23 /* 任務隊列為空,則等待有新任務加入從而被喚醒 */ 24 synchronized (taskQueue) { 25 taskQueue.wait(200); 26 } 27 } catch (InterruptedException ie) { 28 } 29 } 30 ArrayList<TaskModel> taskModels; 31 synchronized (taskQueue) { 32 //隊列不為空的情況下 取出隊列定時器任務 33 taskModels = new ArrayList<>(taskQueue); 34 } 35 if (!taskModels.isEmpty()) { 36 for (TaskModel task : taskModels) { 37 TimerTask timerEvent = (TimerTask) task; 38 int execCount = timerEvent.getRunAttribute().getintValue("Execcount"); 39 long lastTime = timerEvent.getRunAttribute().getlongValue("LastExecTime"); 40 long nowTime = System.currentTimeMillis(); 41 if (nowTime > timerEvent.getStartTime() //是否滿足開始時間 42 && (nowTime - timerEvent.getSubmitTime() > timerEvent.getIntervalTime())//提交以后是否滿足了間隔時間 43 && (timerEvent.getEndTime() <= 0 || nowTime < timerEvent.getEndTime()) //判斷結束時間 44 && (nowTime - lastTime >= timerEvent.getIntervalTime())) //判斷上次執行到目前是否滿足間隔時間 45 { 46 //提交執行 47 ThreadManager.getInstance().addTask(timerEvent.gettID(), timerEvent); 48 //記錄 49 execCount++; 50 timerEvent.getRunAttribute().put("Execcount", execCount); 51 timerEvent.getRunAttribute().put("LastExecTime", nowTime); 52 } 53 nowTime = System.currentTimeMillis(); 54 //判斷刪除條件 55 if ((timerEvent.getEndTime() > 0 && nowTime < timerEvent.getEndTime()) 56 || (timerEvent.getActionCount() > 0 && timerEvent.getActionCount() <= execCount)) { 57 taskQueue.remove(task); 58 } 59 } 60 } 61 try { 62 //定時器, 執行方式 間隔 4ms 執行一次 把需要處理的任務放到對應的處理線程 63 Thread.sleep(4); 64 } catch (InterruptedException ex) { 65 } 66 } 67 log.error("線程結束, 工人<“" + Thread.currentThread().getName() + "”>退出"); 68 } 69 70 @Override 71 public void addTask(TaskModel task) { 72 if (((TimerTask) task).isIsStartAction()) { 73 try { 74 task.run(); 75 } catch (Exception e) { 76 log.error("工人<“" + Thread.currentThread().getName() + "”> 執行任務<" + task.getID() + "(“" + task.getName() + "”)> 遇到錯誤: " + e); 77 e.printStackTrace(); 78 } 79 } 80 super.addTask(task); 81 } 82 }
定時器線程執行任務模型
1 /** 2 * 定時器執行器 3 * 4 * @author 失足程序員 5 * @Blog http://www.cnblogs.com/ty408/ 6 * @mail 492794628@qq.com 7 * @phone 13882122019 8 */ 9 public abstract class TimerTask extends TaskModel { 10 11 private static final long serialVersionUID = -8331296295264699207L; 12 13 /** 14 * 線程ID 15 */ 16 private long tID; 17 /** 18 * 開始執行的時間 19 */ 20 private long startTime; 21 22 /** 23 * 是否一開始執行一次 24 */ 25 private boolean isStartAction; 26 27 /** 28 * 結束時間 29 */ 30 private long endTime; 31 32 /** 33 * 執行次數 34 */ 35 private int actionCount; 36 37 /** 38 * 間隔執行時間 39 */ 40 private int intervalTime; 41 42 /** 43 * 44 * @param tID 指定執行線程 45 * @param startTime 指定開始時間 46 * @param isStartAction 是否一開始就執行一次 47 * @param endTime 指定結束時間 48 * @param actionCount 指定執行次數 49 * @param intervalTime 指定間隔時間 50 * @param ID 51 * @param Name 52 */ 53 public TimerTask(long tID, long startTime, boolean isStartAction, long endTime, int actionCount, int intervalTime, long ID, String Name) { 54 super(ID, Name); 55 this.tID = tID; 56 this.startTime = startTime; 57 this.isStartAction = isStartAction; 58 this.endTime = endTime; 59 this.actionCount = actionCount; 60 this.intervalTime = intervalTime; 61 } 62 63 /** 64 * 指定任務的開始執行時間 65 * 66 * @param tID 指定執行線程 67 * @param startTime 指定開始時間 68 * @param isStartAction 是否一開始就執行一次 69 * @param actionCount 指定執行次數 70 * @param intervalTime 指定間隔時間 71 * @param ID 72 * @param Name 73 */ 74 public TimerTask(long tID, long startTime, boolean isStartAction, int actionCount, int intervalTime, long ID, String Name) { 75 this(tID, startTime, isStartAction, 0, actionCount, intervalTime, ID, Name); 76 } 77 78 /** 79 * 指定結束時間已結束時間為准,執行次數不一定夠 80 * 81 * @param tID 指定執行線程 82 * @param isStartAction 是否一開始就執行一次 83 * @param endTime 指定結束時間 84 * @param actionCount 指定執行次數 85 * @param intervalTime 指定間隔時間 86 * @param ID 87 * @param Name 88 */ 89 public TimerTask(long tID, boolean isStartAction, long endTime, int actionCount, int intervalTime, long ID, String Name) { 90 this(tID, 0, isStartAction, endTime, actionCount, intervalTime, ID, Name); 91 } 92 93 /** 94 * 指定開始時間,和結束時間 95 * 96 * @param tID 指定執行線程 97 * @param startTime 指定開始時間 98 * @param endTime 指定結束時間 99 * @param intervalTime 指定間隔時間 100 * @param ID 101 * @param Name 102 */ 103 public TimerTask(long tID, long startTime, long endTime, int intervalTime, long ID, String Name) { 104 this(tID, startTime, false, endTime, -1, intervalTime, ID, Name); 105 } 106 107 /** 108 * 指定執行線程,指定執行次數,指定間隔時間 109 * 110 * @param tID 指定執行線程 111 * @param actionCount 指定執行次數 112 * @param intervalTime 指定間隔時間 113 * @param ID 114 * @param Name 115 */ 116 public TimerTask(long tID, int actionCount, int intervalTime, long ID, String Name) { 117 this(tID, 0, false, 0, actionCount, intervalTime, ID, Name); 118 } 119 120 /** 121 * 指定的執行次數和間隔時間 122 * 123 * @param actionCount 指定執行次數 124 * @param intervalTime 指定間隔時間 125 */ 126 public TimerTask(int actionCount, int intervalTime) { 127 this(0, 0, false, 0, actionCount, intervalTime, 0, "無名"); 128 } 129 130 /** 131 * 提交后指定的時間以后執行一次 132 * 133 * @param intervalTime 指定間隔時間 134 */ 135 public TimerTask(int intervalTime) { 136 this(0, 0, false, 0, 1, intervalTime, 0, "無名"); 137 } 138 139 public long gettID() { 140 return tID; 141 } 142 143 public void settID(long tID) { 144 this.tID = tID; 145 } 146 147 public long getStartTime() { 148 return startTime; 149 } 150 151 public void setStartTime(long startTime) { 152 this.startTime = startTime; 153 } 154 155 public boolean isIsStartAction() { 156 return isStartAction; 157 } 158 159 public void setIsStartAction(boolean isStartAction) { 160 this.isStartAction = isStartAction; 161 } 162 163 public long getEndTime() { 164 return endTime; 165 } 166 167 public void setEndTime(long endTime) { 168 this.endTime = endTime; 169 } 170 171 public int getActionCount() { 172 return actionCount; 173 } 174 175 public void setActionCount(int actionCount) { 176 this.actionCount = actionCount; 177 } 178 179 public int getIntervalTime() { 180 return intervalTime; 181 } 182 183 public void setIntervalTime(int intervalTime) { 184 this.intervalTime = intervalTime; 185 } 186 187 }
創建一個線程管理器

1 /** 2 * 線程管理器 3 * 4 * @author 失足程序員 5 * @Blog http://www.cnblogs.com/ty408/ 6 * @mail 492794628@qq.com 7 * @phone 13882122019 8 * 9 */ 10 public class ThreadManager { 11 12 private static final Logger log = Logger.getLogger(ThreadManager.class); 13 14 private static final ThreadGroup GlobeThreadGroup = new ThreadGroup("全局線程"); 15 private static final ThreadGroup lsThreadGroup = new ThreadGroup("零時線程"); 16 17 private static ThreadManager instance = new ThreadManager(); 18 19 private static final HashMap<Long, ThreadModel> workThreadMaps = new HashMap<>(0); 20 21 public static ThreadManager getInstance() { 22 return instance; 23 } 24 25 public static ThreadGroup getGlobeThreadGroup() { 26 return GlobeThreadGroup; 27 } 28 29 private final BackThread backThread = new BackThread(); 30 31 private final TimerThread timerThread = new TimerThread(); 32 33 //服務器是否運行狀態標識 34 private boolean running = true; 35 36 public boolean isRunning() { 37 return running; 38 } 39 40 public void StopServer() { 41 running = false; 42 } 43 44 public long addThread(ThreadModel thread) { 45 workThreadMaps.put(thread.getId(), thread); 46 thread.start(); 47 return thread.getId(); 48 } 49 50 public long getThread(ThreadGroup threadGroup, String workName) { 51 return addThread(new ThreadModel(threadGroup, workName)); 52 } 53 54 public long getThread(String workName) { 55 return addThread(new ThreadModel(lsThreadGroup, workName)); 56 } 57 58 public boolean delete(long threadID) { 59 ThreadModel get = workThreadMaps.remove(threadID); 60 if (get != null) { 61 get.setRuning(false); 62 return true; 63 } 64 return false; 65 } 66 67 public void addTask(long threadID, TaskModel task) { 68 if (workThreadMaps.containsKey(threadID)) { 69 workThreadMaps.get(threadID).addTask(task); 70 } else { 71 addBackTask(task); 72 } 73 } 74 75 public void addTimerTask(TimerTask task) { 76 timerThread.addTask(task); 77 } 78 79 /** 80 * 81 * @param task 82 */ 83 public void addBackTask(TaskModel task) { 84 backThread.addTask(task); 85 } 86 87 }
重新測試一下
1 public static void main(String[] args) { 2 3 long thread = ThreadManager.getInstance().getThread("Test"); 4 ThreadManager.getInstance().addBackTask(new TaskModel() { 5 6 @Override 7 public void run() { 8 System.out.println("addBackTask"); 9 } 10 }); 11 ThreadManager.getInstance().addTimerTask(new TimerTask(5, 100) { 12 13 @Override 14 public void run() { 15 System.out.println("TimerTask 5 100"); 16 } 17 }); 18 ThreadManager.getInstance().addTask(thread, new TaskModel() { 19 20 @Override 21 public void run() { 22 System.out.println("Thread test"); 23 } 24 }); 25 }
1 [04-24 17:40:23:0883:INFO : sz.network.threadpool.BackThread:37 行] -> ---初始化后台線程池--線程數量:10------------ 2 addBackTask 3 Thread test 4 [04-24 17:40:23:0888:INFO : sz.network.threadpool.BackThread:89 行] -> 工人<“后台線程-10”> 完成了任務:TaskModel{ID=0, Name=無名, runAttribute={submitTime=1429868423887}} 執行耗時:0 提交耗時:0 5 [04-24 17:40:23:0888:INFO : sz.network.threadpool.TaskModel:97 行] -> 工人<“Test”> 完成了任務:TaskModel{ID=0, Name=無名, runAttribute={submitTime=1429868423887}} 執行耗時:1 提交耗時:1 6 TimerTask 5 100 7 [04-24 17:40:23:0988:INFO : sz.network.threadpool.BackThread:89 行] -> 工人<“后台線程-9”> 完成了任務:TaskModel{ID=0, Name=無名, runAttribute={LastExecTime=1429868423988, submitTime=1429868423887, Execcount=1}} 執行耗時:0 提交耗時:101 8 TimerTask 5 100 9 [04-24 17:40:24:0088:INFO : sz.network.threadpool.BackThread:89 行] -> 工人<“后台線程-8”> 完成了任務:TaskModel{ID=0, Name=無名, runAttribute={LastExecTime=1429868424088, submitTime=1429868423887, Execcount=2}} 執行耗時:0 提交耗時:201 10 TimerTask 5 100 11 [04-24 17:40:24:0189:INFO : sz.network.threadpool.BackThread:89 行] -> 工人<“后台線程-7”> 完成了任務:TaskModel{ID=0, Name=無名, runAttribute={LastExecTime=1429868424189, submitTime=1429868423887, Execcount=3}} 執行耗時:0 提交耗時:302 12 TimerTask 5 100 13 [04-24 17:40:24:0289:INFO : sz.network.threadpool.BackThread:89 行] -> 工人<“后台線程-6”> 完成了任務:TaskModel{ID=0, Name=無名, runAttribute={LastExecTime=1429868424289, submitTime=1429868423887, Execcount=4}} 執行耗時:0 提交耗時:402 14 TimerTask 5 100 15 [04-24 17:40:24:0389:INFO : sz.network.threadpool.BackThread:89 行] -> 工人<“后台線程-9”> 完成了任務:TaskModel{ID=0, Name=無名, runAttribute={LastExecTime=1429868424389, submitTime=1429868423887, Execcount=5}} 執行耗時:0 提交耗時:502
調試查看線程信息
Java版本的自定義線程處理器就算完成了。
功能和實現基本和C#版本一樣。