Multithreading

I am just starting to learn multithreading. My second exercise was to put 20 balls on the screen. Each one is moved using a different thread. On the surface I was surprised what little computer resources the 20 threads used. My machine is dedicating 3 percent of its CPU to the bouncing balls app.

I have some nice and smooth animation too. Every frame update causes the whole screen to be repainted. Right now I am using the basic Thread class to start up and Runnable. My main thread, which I assume is the event dispatch thread, updates the GUI on a timer (which happens to have its own thread).

Next I should try a whole lot more threads. Or I could make the graphics more intense. I need to figure out where the performance bottleneck is, if any.

Eating the Keys

I am learning about the finer points of GUI development with Java this week. It turns out there are some GUI basics in Java that I don't know about. So I am studying them first. One topic of interest is catching events. To practive, I started coding up an event viewer. It is supposed to get and print out events that fire based on a bunch of different GUI components.

For the most part I can knock out the even viewer code. I am setting up listeners that log events to the screen. Then I try to do a KeyListener. But my event handlers are not getting called. What the heck? Initially my text area control was displaying the keys. Okay. I disabled it. Still no keyboard events.

I am a good debugger. I traced the issue down to the presence of a JButton on my GUI. If I remove the JButton, the keyboard events fire and I get the calls. What could be going on here? Is the JButton receiving and eating up the keyboard events? It looks like I am going to have to consult an expert here.

The Need for Wildcards

We are covering generics in my advanced Java community college class. The weak book has a tough time explaining concepts clearly. I get the main idea behind generics. Java gets to keep up with C++ templates. But then we get to something which does not seem right.

Since the Integer class extends Number, you can pass in an Integer object when a function requires a Number parameter. That is clear. But when you use those types with a generic class such as Stack, things get weird. You cannot pass in an object of type Stack when a function requires a Stack parameter. What the heck?

Integer extends Number. The Stack is a Stack. Why can't Stack act as a Stack. Makes no sense on the outside. The book cooked up some example to try to explain it away. I was still not convinced. Later I read about the use of wildcards to solve this problem. You get a Stack argument type that can then receive a Stack. But why do we need this new syntax in the first place. The textbook could not explain it. Time to dig deeper.