內執行的線程我有一個與while(true)
循環總是一個線程,基本上它所做的就是Runnable對象添加到一個執行者。執行人不是從主線程
OrderExecutionThread:
public class OrderExecutionThread extends Thread implements Runnable {
final private static int ORDER_EXEC_THREADS_NUMBER = 10;
private boolean running = true;
private boolean flag = true;
private List<Order> firstSellsList = new ArrayList<>();
private List<Order> secondSellsList = new ArrayList<>();
private ManagedDataSource managedDataSource;
private ExecutorService executorService;
public OrderExecutionThread(ManagedDataSource managedDataSource) {
this.managedDataSource = managedDataSource;
this.executorService = Executors.newFixedThreadPool(ORDER_EXEC_THREADS_NUMBER);
}
@Override
public void run() {
while (running) {
if (!firstSellsList.isEmpty() && !firstBuysList.isEmpty()) {
initAndRunExecution(firstBuysList.get(0), firstSellsList.get(0));
}
}
private void initAndRunExecution(Order buy, Order sell) {
executorService.submit(new OrderExecution(buy, sell, managedDataSource));
}
}
我跑這個線程通過我的主類這樣做:
new Thread(orderExecutionThread).start();
假設執行人執行OrderExecution runnable
對象,做到這一點:
@Override
public void run() {
try {
connection = managedDataSource.getConnection();
makeExecution(sell, buy);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (!connection.isClosed())
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我知道這兩個清單都不是空的,initAndRunExecution
被調用,但是爲了執行run方法不會被調用....
嘗試'firstBuysList.remove(0)'代替'firstBuysList.get(0)'。 firstSells一樣...'get'不會刪除元素。所以你處於無限循環中,以非常高的頻率提交相同的兩個元素。所以我懷疑你的Executor線程根本就不會被調度。 – Fildor
@Fildor,刪除不工作以及... – kitsuneFox
哦,我看得好。刪除了我的評論。你如何填寫這些列表? – JIV