Monday, June 23, 2014

Think Anagrams Are Easy?

So did I until I wrote my new education app 'Alien Anagrams'.  It's a sort-of shoot'em-up, except you shoot bullets by correctly guessing anagrams.

Take the anagram in the screenshot above: "lseo".  it took me a good few minutes to guess it, when I would have assumed a 4-letter anagram would take about 4 seconds.  How many combinations can there be?  But for some reason my brain keeps thinking of words like "leo's" or "selo"[tape].  Still, that's why I like playing this game; if you lose it's your own fault, and when the computer shows you the word, it suddenly becomes obvious what the answer was.

Give it a go (the first few levels are free, and they are challenging enough) and see how well you do.

Friday, June 13, 2014

System-Wide Mutex

It's been a long long time since I actually posted anything directly to do with Java programming, so here I am to make up for that.

In one of my projects I needed to create a system-wide mutex (a "mutex" being an object or "flag" to say that something is locked, e.g. a method or bit of code, so no other process can call it).  I needed it to be system-wide since I had multiple programs running on the same computer that needed to be in sync, so I couldn't use the synchronized keyword.

So I created a new class which I've called  FileSystemMutex.  It's very simple, and works by creating a temporary file to indicate that the lock is enabled and the file is deleted when the lock is released.  Because it uses the filesystem, it is "global" across everything that uses that filesystem.

Here is the code:-


public class FileSystemMutex {
    private File file;
    private long max_duration;
    public FileSystemMutex(String _filename, long _max_duration) {
        file = new File(_filename);
        max_duration = _max_duration;
    public void waitForLock() throws IOException {
        while (isLocked()) {

            // This is my own function.
            // Surely everyone's written their own "Thread.sleep()" 
            // wrapper by now?
            Functions.delay(100); // Wait for a sec 
private boolean isLocked() {
        boolean exists = file.exists();
        if (exists) {

            // It might exist but is it too old?
            long age = file.lastModified();
            if (age + max_duration < System.currentTimeMillis()) {
                // Too old!
                release(); // Delete the file
                return false;
            } else {
                return true;
        } else {
            return false;
    private void lock() throws IOException {
    public void release() {


One thing to always remember with locks it to ensure that under no circumstances should the lock get left on, usually by a program ending prematurely.  This is why I've included the failsafe "maximum duration" parameter, so if the process that created the lock "crashes", the lock will still be released, eventually.

You would use it like this:-

    private static FileSystemMutex fsmutex = 
        new FileSystemMutex("my_lock", 1000*60); // Might as well make it static

    public void MyFunction() {
        try {
            [ do something that requires locking this bit of the code]

        } finally {


'Tis all.

Thursday, June 12, 2014

Intelligent Traffic Alerts

One of the biggest problems with traffic alert apps is actually finding the relevant traffic alerts.  Most apps give you lists of roads or maps, and you have to spend the time either going through all the roads you might be travelling on, or examining maps to see which road the alert is actually on, and whether it's even on the section of that road you will be using.

That's why I wrote Intelligent Traffic Alerts, which takes all the work out of finding relevant traffic alerts.  Simply turn it on, ensure your GPS is active, and listen.  It will constantly scan for new traffic alerts, and if there are any in your vicinity, speak them out to you.

The other major advantage to this is that you don't have to take your eyes off the road.  Even SatNavs can be dangerous as you try and translate the image on the SatNav map to the real world you are on.  With this new app, you get told about the traffic alerts while you drive, which has to be the safest way.  The onlydrawback is that there are no interesting screenshots because there's not a lot to show!