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/cn.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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP