public class ReadFromQueue
{
order = orderFromQueue;
if(!Repository.ordersIdMap.containsKey(order.orderID))
{
Platform.runLater(new Runnable()
{
@Override public void run()
{
Repository.ordersCollection.add(order);
}
}
});
Repository.ordersIdMap.put(order.orderID, order);
}
嗨,再次,我提出了另一个问题,因为前一个问题......很糟糕(对不起) 让我告诉你这个场景。你有一个读者进入一个队列并获得一个订单(让我们说它是一个订单对象,准备使用),这个东西工作得如此之快,每秒钟获得大量订单..(确切地说,我得到了40,000个订单不到一分钟)..我有一个单例类的Repository,在这个类中我有一个ordersIdMap(Key String和值Order的ConcurrentHashMap)和一个ObservableList(我的tableView的源代码)的ordersCollection。我不能添加订单,如果已经存在于集合中,那么在我的地图中我将orderId(字符串)保存为密钥,这样如果再次出现相同的订单,我必须更新它(否则代码不在这里,但是现在不是重要的)。问题是调用Platform.runLater绘制UI是给我带来问题的原因。为什么?因为如果我去获得另一个订单并且Platform.runLater还没有完成..订单没有在地图上创建,所以我的“读者”订单是新的并再次创建它(在这种情况下订单有相同的orderId),所以我一遍又一遍得到相同的订单..我必须说有时候它足够快,订单得到更新..但大多数时间太慢而且订单再次创建。我还试图把“Repository.ordersIdMap.put(order.orderID,order);”就在if条件旁边..这样地图就会有钥匙,无论怎样......但是仍然无法工作(为什么......?dunno)..另外如果我不使用platform.runlater ..its的作品但是我得到了很多NullPointersException ..因为我试图更新UI以快速..我想..任何答案都是有用的!!谢谢!抱歉我的英语。
编辑:这是整个代码.. execRpt就像一个“小订单”,从中我可以创建一个更大的订单..我收到许多execRpt,然后创建订单..如果订单存在更新。 .if没有添加它,但它更新失败(有些时候),只是添加了新的订单。package com.larrainvial.trading.trademonitor.listeners;
import com.larrainvial.trading.emp.Controller;
import com.larrainvial.trading.trademonitor.Repository;
import com.larrainvial.trading.emp.Event;
import com.larrainvial.trading.emp.Listener;
import com.larrainvial.trading.fix44.events.ReceivedExecutionReportEvent;
import com.larrainvial.trading.trademonitor.events.CalculatePositionsEvent;
import com.larrainvial.trading.trademonitor.vo.ExecRptVo;
import com.larrainvial.trading.trademonitor.vo.OrderVo;
import javafx.application.Platform;
import javafx.concurrent.Task;
import quickfix.FieldNotFound;
import quickfix.fix44.ExecutionReport;
public class ReceivedExecutionReportToMonitorListener implements Listener
{
private OrderVo orderVo;
private String ordStatus = "";
private String transactTime = "";
private String text = "";
private int qty = 0;
private int cumQty = 0;
private int lastQty = 0;
private int leavesQty = 0;
private double price = 0;
private double avgPx = 0;
private double lastPx = 0;
@Override
public void eventOccurred(Event event)
{
ExecutionReport executionReport = ((ReceivedExecutionReportEvent)event).message;
try
{
String settlType = "";
String orderID = executionReport.isSetOrderID()? String.valueOf(executionReport.getOrderID().getValue()) : "";
String execID = executionReport.isSetExecID()? String.valueOf(executionReport.getExecID().getValue()) : "";
String execType = executionReport.isSetExecType()? String.valueOf(executionReport.getExecType().getValue()) : "";
String clOrdID = executionReport.isSetClOrdID()? String.valueOf(executionReport.getClOrdID().getValue()) : "";
String clOrdLinkID = executionReport.isSetClOrdLinkID()? String.valueOf(executionReport.getClOrdLinkID().getValue()) : "";
transactTime = executionReport.isSetTransactTime() ? String.valueOf(executionReport.getTransactTime().getValue()) : "";
text = executionReport.isSetText() ? executionReport.getText().getValue().toString() : "";
String tif = executionReport.isSetTimeInForce() ? String.valueOf(executionReport.getTimeInForce().getValue()) : "";
String handlInst = executionReport.isSetHandlInst() ? String.valueOf(executionReport.getHandlInst().getValue()) : "";
String securityExchange = executionReport.isSetSecurityExchange()? String.valueOf(executionReport.getSecurityExchange().getValue()) : "";
String orderType = executionReport.isSetOrdType()? String.valueOf(executionReport.getOrdType().getValue()) : "";
String account = executionReport.isSetAccount() ? String.valueOf(executionReport.getAccount().getValue()) : "None";
ordStatus = String.valueOf(executionReport.getOrdStatus().getValue());
lastPx = executionReport.isSetLastPx()? executionReport.getLastPx().getValue() : 0;
price = executionReport.isSetPrice()? executionReport.getPrice().getValue() : 0;
avgPx = executionReport.isSetAvgPx()? executionReport.getAvgPx().getValue() : 0;
lastQty = executionReport.isSetLastQty()? (int)executionReport.getLastQty().getValue() : 0;
leavesQty = executionReport.isSetLeavesQty()? (int)executionReport.getLeavesQty().getValue() : 0;
cumQty = executionReport.isSetCumQty()? (int)executionReport.getCumQty().getValue() : 0;
qty = executionReport.isSetOrderQty()? (int)executionReport.getOrderQty().getValue() : 0;
ExecRptVo execRpt = new ExecRptVo(orderID,
execID,
execType,
ordStatus,
clOrdID,
clOrdLinkID,
securityExchange,
String.valueOf(executionReport.getSide().getValue()),
qty,
lastQty,
leavesQty,
cumQty,
executionReport.getSymbol().getValue().toString(),
orderType,
price,
lastPx,
avgPx,
tif,
"",
handlInst,
securityExchange,
settlType,
account,
text,
transactTime);
orderVo = new OrderVo(execRpt);
OrderVo orderExist = Repository.ordersIdMap.putIfAbsent(orderID, orderVo);
if(orderExist == null)
{
Platform.runLater(new Runnable()
{
@Override public void run()
{
Repository.ordersCollection.add(orderVo);
}
});
}
else
{
Repository.ordersIdMap.get(orderID).price.set(price);
Repository.ordersIdMap.get(orderID).qty.set(qty);
Repository.ordersIdMap.get(orderID).ordStatus.set(ordStatus);
Repository.ordersIdMap.get(orderID).transactTime.set(transactTime);
Repository.ordersIdMap.get(orderID).text.set(text);
if(avgPx > 0)
Repository.ordersIdMap.get(orderID).avgPx.set(avgPx);
if(cumQty > 0)
Repository.ordersIdMap.get(orderID).cumQty.set(cumQty);
if(lastQty > 0)
Repository.ordersIdMap.get(orderID).lastQty.set(lastQty);
if(lastPx > 0)
Repository.ordersIdMap.get(orderID).lastPx.set(lastPx);
if(leavesQty > 0)
Repository.ordersIdMap.get(orderID).leavesQty.set(leavesQty);
if(ordStatus.equals("8"))
Repository.ordersIdMap.get(orderID).rejected.set("1");
Repository.ordersIdMap.get(orderID).execRpts.add(execRpt);
}
if(execType.equals("1") || execType.equals("2") || execType.equals("F"))
{
CalculatePositionsEvent calculatePositionsEvent = new CalculatePositionsEvent(execRpt);
Controller.dispatchEvent(calculatePositionsEvent);
}
}
catch (FieldNotFound ex)
{
ex.printStackTrace();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
答案 0 :(得分:2)
代码中的一个问题是你有一个非原子的check-then-act模式。一种解决方法是:
order = orderFromQueue;
Order alreadyInMap = Repository.ordersIdMap.putIfAbsent(order.orderID, order);
if(alreadyInMap == null) { //it really is a new order
Platform.runLater(new Runnable() {
@Override public void run() {
Repository.ordersCollection.add(order);
}
});
}
还要注意“因为如果我去获得另一个订单并且Platform.runLater还没有完成..订单没有在地图上创建”没有意义:当你调用Platform.runLater()
,runnable被放入队列但是异步执行(除非你的方法已在FX Thread上运行)。