Synchronization solution needed

Discussion about JFreeChart related to stockmarket charts.
Locked
bfry5282
Posts: 10
Joined: Thu Sep 02, 2004 2:45 am
Location: Durham, NC USA
Contact:

Synchronization solution needed

Post by bfry5282 » Sat Sep 18, 2004 12:40 pm

OK, posting this in the stock forum...

Exception message shown below. But I've been thinking about this, and perhaps here is a solution when streaming data into a dynamic TimeSeries object? I'm pretty sure I should not be asynchronously updating data structures for JFreeChart from various data feeder threads. So, I'm thinking this technique may allow me to do it in a serialized "safe" fashion? Comments anyone?

Since I'm using a Java application, and the AWT is involved, should I solve my synchronization and data corruption problems by updating the TimeSeries object on the GUI thread itself, which should serialize the operations, and therefore not corrupt data structures?

From javax.swing.SwingUtilities javadoc:

public static void invokeLater(Runnable doRun)

Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.

Runnable doTimeSeriesUpdate = new Runnable() {
public void run() {
// *************** code here to batch updates to my timeseries values ?? ************************
// update backlog of data to TimeSeries object
System.out.println("TimeSeries update on " + Thread.currentThread());
}
};

SwingUtilities.invokeLater(doTimeSeriesUpdate); // call this periodically or as required
// execution continues -- no wait

*********** posted originally in the general forum *******************

Posted: Mon Sep 06, 2004 6:29 pm Post subject: Dynamic timeseries synchronization?

--------------------------------------------------------------------------------

I'm pumping data into synchronized data structures TimeSeries objects, and using "worker" threads to periodically synch on the various TimeSeries objects while I add data to them. Occasionally, I get the exception shown below.

So, I'm wondering what object I need to synch on to ensure the integrity of the charting: the plot object, the chart object, the timeseries object, the dataset object? Can I determine when critical data structures are being accessed, and so I can defer updates, etc.

In general, with data streaming into charting, what is the best synchronization policy?

email: bfry@mindspring.com or trader@TradeWithConfidence.com

bfry5282
Posts: 10
Joined: Thu Sep 02, 2004 2:45 am
Location: Durham, NC USA
Contact:

original data corruption exception

Post by bfry5282 » Sat Sep 18, 2004 12:44 pm

I'm pumping data into synchronized data structures TimeSeries objects, and using "worker" threads to periodically synch on the various TimeSeries objects while I add data to them. Occasionally, I get the exception shown below.

So, I'm wondering what object I need to synch on to ensure the integrity of the charting: the plot object, the chart object, the timeseries object, the dataset object? Can I determine when critical data structures are being accessed, and so I can defer updates, etc.

In general, with data streaming into charting, what is the best synchronization policy?



java.lang.IndexOutOfBoundsException: Index: 615, Size: 615
at java.util.ArrayList.RangeCheck(ArrayList.java:507)
at java.util.ArrayList.get(ArrayList.java:324)
at org.jfree.data.time.TimeSeries.getDataItem(TimeSeries.java:277)
at org.jfree.data.time.TimeSeriesCollection.getYValue(TimeSeriesCollection.java:532)
at org.jfree.data.AbstractXYDataset.getY(AbstractXYDataset.java:75)
at org.jfree.chart.renderer.TWCStandardXYItemRenderer.drawItem(TWCStandardXYItemRenderer.java:99)
at org.jfree.chart.plot.XYPlot.render(XYPlot.java:2204)
at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:1809)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:905)
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:987)
at javax.swing.JComponent.paint(JComponent.java:808)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4795)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4748)
at javax.swing.JComponent._paintImmediately(JComponent.java:4692)
at javax.swing.JComponent.paintImmediately(JComponent.java:4495)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

email: bfry@mindspring.com or trader@TradeWithConfidence.com

adullur
Posts: 1
Joined: Sun Oct 03, 2004 7:46 pm

synchronization problem

Post by adullur » Sun Oct 03, 2004 7:50 pm

I had exactly the same problem. I haven't yet tried to make my application single threaded but did that help you? There is a note in the example with dynamic charts about the approach being unsafe. It says clearly that code that writes to the dataset and the code that updates the charts is not thread-safe and that this issue will be fixed by 1.0. So I guess we do need to wait till then.

bfry5282
Posts: 10
Joined: Thu Sep 02, 2004 2:45 am
Location: Durham, NC USA
Contact:

Synchronization of chart updates

Post by bfry5282 » Tue Oct 12, 2004 5:47 pm

The solution for me is to perform any graph updates on the Swing GUI update thread. That is done by using

SwingUtilities.invokeLater(new UpdateChartThread(bufferedData));

So, I buffer data and periodically a trigger thread invokes the charting update, which runs all operations on the GUI thread, which will serialize the updates and avoid problems.

I don't think it will ever be thread-safe. Swing itself isn't thread-safe, and you must do most if not all GUI-related actions on the gui update thread, as shown above, in order to avoid major problems.

I have fabulous performance, and am able to run nearly 20 simultaneous charts being fed by live market data analysis with no problems at all !!

TradeWithConfidence.com

Locked