I am writing some logs and i want it assynchronously. It is used in multithreaded swing application processing some market price information. When there is huge ammount of comming price tick, it fails to make correct log. It produce only partial log (parts of lines, even parts of words etc.)
class ReceiveLMAXPriceFeedTask extends SwingWorker<Void, Void>
{
...
...
private StringBuilder _log;
private StringBuilder log;
public ReceiveLMAXPriceFeedTask()
{
...
...
_log = new StringBuilder(20000);
log = new StringBuilder(20000);
}
and in place i want to write somethig
...
{
_log.append("xxxxx\n");
_log.append("xxxxx");
}
public Void doInBackground()
{
...
...
while (!isCancelled())
{
...
...
Thread.yield();
if ((_log.length() > 0) && (log.length() == 0))
{
log.append(_log.toString());
firePropertyChange("tradesLog","","trades log changed");
_log.delete(0, _log.length());
}
}
}
And in the code of UI
/**
* Invoked when any task asks for UI update.
*/
public void propertyChange(PropertyChangeEvent evt) {
...
...
else if ("tradesLog" == evt.getPropertyName())
{
try
{
if ((receiveLMAXPriceFeedTask != null) && (receiveLMAXPriceFeedTask.log != null) && (receiveLMAXPriceFeedTask.log.length() > 0))
{
bufferedTradesWriter.write(receiveLMAXPriceFeedTask.log.toString());
bufferedTradesWriter.flush();
receiveLMAXPriceFeedTask.log.delete(0, receiveLMAXPriceFeedTask.log.length());
}
}
catch (IOException ex)
{
System.out.println("problem accessing file" + TradesCSVFilePath.getText());
}
}
I can not find, what caused partial words, lines etc. in logs?