java 板


LINE

首先java的thread真的沒有優先緒 除非要自己做wait/block/join 之類的(雖然API可以去調整,但JVM不保證) 不然一但放下,就是各安天命 不過在此我主要是推測你的需求為何 首先~我倒導 run E after BCD done run BCD after A done 接著導出 run A done then run BCD run BCD done then run E 在得出 run group 1 : A run group 2 : BCD run group 3 : E 以下是sample code 看看有沒有猜對你的需求 -- java 8 package com.swpoker; import java.util.*; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** */ public class Test { static class ExecuteBean{ public String name; //group key public int key; //group parent key public int parentkey; public String memo; public ExecuteBean(String name, int key,int parentkey, String memo){ this.name=name; this. key=key; this. parentkey=parentkey; this. memo=memo; } } static List<ExecuteBean> simulateData(){ List<ExecuteBean> rs = new ArrayList(); //group 1 rs.add(new ExecuteBean("A",1,-1,"A1")); //group 2 rs.add(new ExecuteBean("B",2,1,"B1")); rs.add(new ExecuteBean("C",2,1,"C1")); rs.add(new ExecuteBean("D",2,1,"D1")); //group 3 rs.add(new ExecuteBean("E",3,2,"E1")); //group 3 rs.add(new ExecuteBean("G",3,3,"G1")); return rs; } static class ExecuteActor implements Runnable{ ExecuteBean bean; public ExecuteActor(ExecuteBean bean){ this.bean=bean; } long decidewaittime(){ switch (this.bean.name){ case "A" : return 5*1000; case "B" : return 2*1000; case "C" : return 20*1000; case "D" : return 10*1000; case "E" : return 8*1000; } return 1*1000; } @Override public void run() { try{ long waittime =decidewaittime(); System.out.printf("%s start wait(%s) %s\n",this.bean.memo,waittime, Calendar.getInstance().getTime()); //do something Thread.sleep(waittime); System.out.printf("%s end %s\n",this.bean.memo, Calendar.getInstance().getTime()); }catch (Exception e){e.printStackTrace();} } } static Map<Integer, List<ExecuteBean>> groupExecute( List<ExecuteBean> data){ //implement Comparator if spec Map<Integer, List<ExecuteBean>> groups= new TreeMap(); Integer key; for(ExecuteBean eb : data){ key = eb.key; if(groups.containsKey(key) == false){ groups.put(key,new ArrayList() ); } groups.get(key).add(eb); } return groups; } static void runlists(List<ExecuteBean> executeBeans) throws Exception { //execute pool // choose thread pool ? ScheduledThreadPoolExecutor? ForkJoinPool? ThreadPoolExecutor? ..... ScheduledThreadPoolExecutor pool =new ScheduledThreadPoolExecutor(executeBeans.size()); for(ExecuteBean bean : executeBeans ){ pool.execute(new ExecuteActor(bean)); } pool.shutdown(); //wait depend on pool type pool.awaitTermination(1, TimeUnit.DAYS); } static void rungroups(Map<Integer, List<ExecuteBean>> groups) throws Exception { //key sorted by natural for(Integer key : groups.keySet()){ runlists(groups.get(key)); } } static void execueteWork() throws Exception { //data List<ExecuteBean> data = simulateData() ; //group Map<Integer, List<ExecuteBean>> groups= groupExecute( data) ; //run rungroups(groups); } public static void main(String [] args){ try{ execueteWork(); }catch (Exception e){e.printStackTrace();} } } PS.其實寫到最後才發現我有個地方想錯啦 XD 我這裡是用group ->使用的方法 其實也可以用recursive --> 資料具有的特性 ,結果沒用 XD 寫到最後我才發現 我的資料屬性跟我使用的方法是不一樣的規則 XD ※ 引述《PTTCATKING (懷念美國貓王)》之銘言: : 假設有二十個任務要跑 : 而他在DB裡會有兩組編號(欄位名稱) : 第一組是1~20的流水號 : 第二組是執行優先順序(記錄需等待的對象) : A 跑完 同時執行 B & CD 兩條線同時跑,而B會先跑完,但要等CD都跑完後,BCD都結束 : 才開始跑E : A的編號就是 1 0 (無等待對象) : B的編號就是 2 1 (等待A,1是A的編號) : C的編號就是 3 1 (等待A,1是A的編號) : D的編號就是 4 3 (等待C,3是C的編號) : E的編號就是 5 2 (等待B,2是B的編號) : E的編號就是 5 4 (等待D,4是D的編號) : 因為E要等兩個,所以在DB裡 E 會有兩筆資料 : 請問 有沒有什麼現有套件有能夠執行這件事情呢 : 設定那兩個編號,則程式執行後就按照兩個編號,決定程式優先順序,並有可能同時執行 : 多程序 : 以後維護就是設定編號就能決定跑程式的優先順序 : 今天用CountDownLatch寫出類似的東西,被認為是寫死的東西orz : 這是用排程器???或是什麼方向呢?? @O@ : 能否有高手指點明燈給我方向,謝謝 T_T --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 163.29.28.131
※ 文章網址: http://webptt.com/m.aspx?n=bbs/java/M.1415848161.A.DA9.html







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Soft_Job站內搜尋

TOP