作者swpoker (swpoker)
标题Re: [问题] 请问有办法设定编号来决定排程吗
时间Thu Nov 13 11:09:19 2014
首先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