This isn’t a JOGL only question, but I’d like to ask here since it’s part of a JOGL project and others may have run into this.
I’m currently writing a clock for an animation system that utilizes JOGL. Do to the nature and resolution of my clock, it must be on it’s own thread, separate from the rendering thread. So, it extends Thread and sends ClockEvents to ClockListeners in the main thread. All was well in codeland until I started writing my JUnit test for it.
Since Clock extends Thread, I had to override the public void run() function of Thread to get my functionality. In this case, the run() contains code which does various things, chiefly, putting itself to sleep() for a certain interval, after which, it calls private synchronized sendClockEvent(). OK, wrote a little test program. Works like a charm. So, time to write a JUnit TestCase to see how well it works.
JUnit forces you to think about the different ways you might use an Object – not just what you intended it for. So, since run() is a public method of Thread, what happens when someone calls run()? Well, nothing if someone hasn’t called start() first! So why did the designers of Thread make run() public? Why not just leave it protected so the thread can determine what to do? Anyway, I’m stuck with it. So i thought, maybe put start() in the public Clock() method? Well, that won’t work because you can’t start something that doesn’t exist yet – you eediot! Clock works if someone knows to call start(). But, it seems pretty easy and reasonable for someone to try run(), and Clock will be broken.
How can I hide the run() method of my Clock ( which extends Thread )?