forums Albums groups
Go Back   Betfair Developers Program Forum > Blogs > Java and the Betfair API
Open A Betfair Account
 Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read

Since I mainly access the API using Java, thats what I'll be blogging about. I am most definitely not an expert java programmer, but I am getting very good at reading the Javadoc.

If I post something which is incorrect or something which could be made more efficient somehow, post a comment. I really don't have a problem with criticism of any kind. Even cruel criticism has something to be learned from and since you get a finite number of days allocated to you, any day you don't learn something is one of those days wasted.
Rate this Entry

Concurrently adding and removing components

Posted 03-07-2009 at 04:14 PM by ThanksFish
Updated 03-07-2009 at 04:16 PM by ThanksFish
I've just managed to do something I'm fairly proud of. Its probably fairly small beans to a lot of folk, but it took me ages to figure out and I'm fairly chuffed with myself.

I'm writing a bot (aren't we all!) and I've taken a bit of a leap into the unknown with some of the techniques I'm using. For example, I've created some custom components to show my data. Not all that groundbreaking really, but since the aim of my bot is for me to start it up and walk away from it, but still be able to show me real time information if I come back and look at it. So, I've been trying to figure out how to get it to add and remove the custom components as markets come and go. Again, not terribly groundbreaking if you have done it before.

This is what the main screen looks like, you can see a couple of the components in there.



Well I haven't done it before and I was finding it tricky, not least because at times, there can be multiple threads working with the data coming into the system.

Yes, we are looking at concurrency - a subject which quite a lot of people - me included - find slightly weird and quite difficult to get a handle on.

I can't say I am any good at it yet, but I'm still basking in the glow a lot of programmers get when they finally manage to get a challenging piece of code working.

The idea behind the code is this. On my UI, I have created components which extend JPanel to hold the data I want to see. Each component holds a single market I think I want to monitor. Since I am working with the overs/unders and match odds markets, there can sometimes be a lot of them to monitor. However, some of those markets don't see any action, or are closed well before the match actually finishes. If the market doesn't look like getting much action, I want to be able to stop monitoring it by putting a tick in a box, which will prompt the application to remove the component the next time the loop comes its way. That has the effect of reducing the number of market IDs I will be calling for prices on, which means a) the remaining markets on the form are sorted more efficiently and b) my UI isn't cluttered up with dead markets.

Here is the code. It may not be the most efficient, but it works. I'm going on the basis of 'get it working first, optimise it later'.

Code:
    private void removeMarketFromUI(TwoRunnerMarket pan)
    {
        synchronized (marketsOnForm)
        {
            int mid = pan.getMarketID();
            Iterator it = marketsOnForm.iterator();
            while (it.hasNext())
            {
                synchronized (this.ms.monForm.pnlAllMarkets)
                {
                    Object obj = it.next();
                    if (obj instanceof TwoRunnerMarket)
                    {
                        TwoRunnerMarket onForm = (TwoRunnerMarket) obj;
                        if (onForm.getMarketID() == mid)
                        {
                            this.ms.monForm.pnlAllMarkets.remove(pan);
                            this.ms.monForm.pnlAllMarkets.validate();
                            this.ms.monForm.pnlAllMarkets.notify();
                            marketsOnForm.notify();
                        }
                    }
                }
            }
        }
    }
I'm still puzzling over whether the marketsOnForm variable needs to be synchronized and notified, but I've left it in there just in case. I have a feeling it doesn't, because it is simply a reference to the this.ms.monForm.pnlAllMarkets object, but I'm leaving it in there for the moment. As I said, I'll optimise it later, right now, I want to get it running - which it seems to be doing quite nicely.

Now, maybe I can get on with the university work I have been putting off for ages - it is due in on Monday!
Attached Thumbnails
Click image for larger version

Name:	Capture.JPG
Views:	354
Size:	97.9 KB
ID:	7  
Total Comments 0

Comments

 

All times are GMT. The time now is 03:06 PM.

BETFAIR® and the BETFAIR LOGO are registered trade marks of The Sporting Exchange Limited. Data on Betfair website(s) (including pricing data) is protected by © and database rights. It may not be used for any purpose without a licence. © The Sporting Exchange Limited. All rights reserved.