2010-09-08 59 views
0

要回答這個問題,站在指令執行的批處理需要一點的銀行知識:在不同的流

說我有設置SI從帳戶A1-> a2和A2-> A3 這些是僅有的兩個指令,需要批量處理。 但我希望這兩個指令將在兩個不同的流中處理。

這是不可能的,因爲帳戶a2被鎖定在流1中,並且不能在流2中處理,直到流1完成其處理。

解決此問題的方法: 要麼我在一個流中執行所有SI,要麼確定指令的依賴關係,並相應地將這些指令放入流中。

或者還有其他解決方法。

我正在使用Java和Hibernate。

回答

0

我並不熟悉問題的具體細節,但聽起來很像流水線CPU中的data hazards

在您的情況下,您希望在不引入數據危險的情況下最大限度地利用您的功能單元(流)。也許你可以編寫一個簡單的調度程序,查看下n個指令,並將它們安排到流中?

/** 
* Keeps track of which stream is scheduled to process each account. 
* If an account is not currently scheduled to be processed, it doesn't appear in the keySet of this map. 
*/ 
Map<Account, Stream> accountsInFlight; // use a synchronized implementation 

void schedule(Instruction instruction) { 
    // If the input account is in flight somewhere, use that stream 
    if (accountsInFlight.containsKey(instruction.getInAccount())) { 
    scheduleToStream(instruction, accountsInFlight.get(instruction.getInAccount())); 

    // If the output account is in flight somewhere, use that stream 
    } else if (accountsInFlight.containsKey(instruction.getOutAccount())) { 
    scheduleToStream(instruction, accountsInFlight.get(instruction.getOutAccount())); 

    // If neither are in flight, this is a good candidate for parallel execution, 
    // put it in a different stream 
    } else { 
    Stream stream = // pick a stream (maybe the one with the shortest queue?) 
    scheduleToStream(instruction, stream); 
    } 
} 

private void scheduleToStream(Instruction instruction, Stream stream) { 
    stream.addToQueue(instruction); 
    // Update accountsInFlight 
    accountsInFlight.put(instruction.getInAccount(), stream); 
    accountsInFlight.put(instruction.getOutAccount(), stream); 
} 

// Remove accounts from the map when the instruction completes 

注意,這全是假的代碼:它不是最優化,也沒有正確地涵蓋所有情況,但也許它會給你一些想法開始。

相關問題