0

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jan Blaha
  • 31
  • 4
  • 4
    Look here [`"tradesLog" == evt.getPropertyName()`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Paul Samsotha Sep 02 '14 at 05:53
  • Changed to else if (evt.getPropertyName().equals("tradesLog")) and as I thought, still making wrong partial logs – Jan Blaha Sep 08 '14 at 03:10
  • In above code in question, I have forget to metion, that code producing logs ... { _log.append("xxxxx\n"); _log.append("xxxxx"); } runs in another thread than doInBackground. All such logs producing code is in one thread, started inside doInBackground, but other, than doInBackground itself. – Jan Blaha Sep 08 '14 at 03:21
  • I have changed code to int length = _log.length(); log.append(_log.substring(0,length)); firePropertyChange("tradesLog","","trades log changed"); _log.delete(0, length); Seems to work OK now. Thanks a lot – Jan Blaha Sep 08 '14 at 07:42

0 Answers0