My latest Flickr photograph

CallBackThread for java

Wednesday 24th of December 2008, 03:16:45 pm

I know it's not a kanji sequencing list (真にご免なさい) but it might be useful for some people anyway:

I'd written a java callback thread package to help me write my mahjong program (I needed the players to be genuinely independent, not wait for each other - it made timing issues stupidly hard) and I figured I might as well release it to the general public.

If you've never written callback programs, prepare for confusion: instead of calling each other's methods/functions directly, you send a message going "I need you to ...." and then rather than waiting for a reply, you go about your business until you are interrupted by the person you called with the response to that call.

It comes as a jarchive (with source included), with separate downloads for javadoc and source code, so you can see what happens, and how you might use it yourself.

An interesting detail: this gently rapes java. It uses some perfectly legal java code to - through reflection - bypass java's visibility so that threads don't need to declare protected or private processing methods as public - while not in the hierarchy for whatever object you're writing, this sytem will be able to trigger protected/private processing methods in your objects by using Method.invoke.

It's quite funky really. It also means you need to make sure that you have processing methods for all the messages you're passing around, or you'll get some lovely "No Such Method!" exceptions.

Don't worry, it comes with a fully implemented example so you can see how things work together.

And now I'll go about my snowed-in business again... (go, go, freak winter conditions!)

2 comments - view/add comments