jfreechart changing the dataset and synchronize

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
dunabur
Posts: 17
Joined: Sat Jun 24, 2017 12:05 pm
antibot: No, of course not.

jfreechart changing the dataset and synchronize

Post by dunabur » Fri Apr 27, 2018 12:23 pm

I am developing a swing application with jinternalframes, each of those containing a jfreechart chart. My problem is that all code for adding, removing data from a series is together in SwingUtilities.invokeLater(new Runnable() {...}); code. Since I add and remove a lot from the dataset, it's all being done in one thread and getting really, really slow (like below 1fps) once a few jinternalframes are open.

So I was trying to get at least the code for adding and removing from the dataset out of the event dispatch thread. I thought, by synchronizing on the series, this could be made threadsafe. So this is, what I tried to do in pseudocode:

Code: Select all


synchronized(dataSeries){
	dataSeries.setNotify(false);
	dataSeries.add();
	dataSeries.remove();
}

SwingUtilities.invokeLater(new Runnable() {

	public void run() {
		synchronized(dataSeries){
			dataSeries.setNotify(true);
			chartPanel.restoreAutoBounds();
			domainAxis.setRange(range);
			domainAxis.setRange(range);
			rangeAxis.configure();
			renderer.setCandleWidth(candleWidth);
			(etc...)
			}
		}
So basically I am trying to populate / change the series in one thread, and then draw the chart in the event dispatch thread. I am using synchronize(dataSeries) so that there won't be a change in dataSeries while the chart is being drawn. I use series.setNotify(false) so that no chart update is triggered while changing the dataset. But I still get java.lang.IndexOutOfBoundsException.
I am a bit puzzled, because I thought restricting access to dataSeries should be enough but it is not. Should I use another object to synchronize on? Or is there a better way to split data adding, removal, and chart drawing?

sonnenhohl
Posts: 2
Joined: Tue Apr 24, 2018 8:13 pm
antibot: No, of course not.

Re: jfreechart changing the dataset and synchronize

Post by sonnenhohl » Fri Apr 27, 2018 6:06 pm

Hello dunabur,

For fixing this kind of problem i created two threads one that captures data and put it into a ConcurrentLinkedQueue<> and other thread that runs a little bit more slowly to read the data from this queue and draw it into the screen.

Post Reply