作者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://www.ptt.cc/bbs/java/M.1415848161.A.DA9.html