我有一個迭代客戶 id 的 for 回圈,并且我有在同一個 for 回圈中處理這些客戶的流程函式。
設想:-
在執行for回圈時,如果找到重復客戶,那么我們應該檢查客戶的舊流程是否完成。如果完成,那么我們必須等待。
在這里,我不想停止整個 for 回圈執行,因為可以處理具有不同 ID 的其他客戶。
那么有什么方法可以讓我只停止等待特定客戶的部分行程,同時使用相同的for回圈執行其他行程。
例子
for(customer:customers)
{
if(customer.isRepeated())
{
if(customer.inProcess())
{
wait();
}
}
process(customer);
}
uj5u.com熱心網友回復:
你可以使用Deque。
Deque<Customer> toProcess;
//inialize and populate the queue
while( toProcess.size() > 0){
//take the front
Customer c = toProcess.remove();
if( c.inProcess() ){
//put it back at the tail.
toProcess.offer(c);
continue;
}
//operate on it.
}
uj5u.com熱心網友回復:
對于您描述的問題,另一個答案中的多執行緒方法可能是理想的。但是,如果您想讓它保持單執行緒并在處理準備好的客戶的同時推遲對尚未準備好的客戶的處理,那么這可能會起作用。
在這里,我們創建了另一個串列waitingCustomers,這是我們將拜訪的客戶的串列.wait()。當我們遇到需要等待的客戶時,我們沒有這樣做,而是將其放入waitingCustomers串列中。然后我們重復整個代碼塊,直到waitingCustomers串列為空。
List<Customer> waitingCustomers = customers;
do {
List<Customer> toProcess = waitingCustomers;
waitingCustomers = new ArrayList<>();
for(customer : toProcess) {
if(customer.isRepeated() && customer.inProcess()) {
waitingCustomers.add(customer);
} else {
process(customer);
}
}
} while (waitingCustomers.size() > 0)
uj5u.com熱心網友回復:
您可以為此使用某種異步編程:
new Thread(new Runnable() {
public void run() {
//Do whatever
}
}).start();
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490769.html
上一篇:錯誤:com.microsoft.sqlserver.jdbc.SQLServerException:索引2超出范圍
