Tuesday, 1 November 2011

Java: Shared Queue's 3: Testing Classes

Apparently, no one did shit, so I guess discussing that is outta the question :P So straight onto how I tested it! I'll put both the consumer and producer classes into an umbrella class just so its all together. I'll leave the actual running of the tests to tomorrow, since I'm a bum (or a tortoise?) and haven't actually like, got there yet. I mean, for your benefit, so we aren't moving too fast. Clearly.

I do have the actual classes to be used in the testing though, and here they are:

Of course our class will extend Thread, because we want it to be multithreaded. The constructor just assigns values to the global variables that we'll be using to test, and then we have the thread running method.
In this method, we have a couple of new things. First, assert, which just makes sure there isn't some serious bullshit going on with our queue size. I don't think its particularly important, I just thought I'd include it because I saw it somewhere.

Then, our standard loop. What we want from the loop is to go ahead and add produced values to the shared queue, then hold off and allow another thread to run. That's what the yield() method does. Everything else should be self-explanatory.

Here we have the Consumer, which is about as simple as the Producer. We have our globals and then our run(). This "while (true) ..." is an infinite loop, since true is never being changed. This allows the loop to keep running. If we had a variable to run the loop only as many times as the queue in question had values, it wouldn't be able to account for newly added values, which is a bummer.

To solve this infinite loop, though, we just have a break statement if the conditions aren't met (the queue being empty). Otherwise, this is simple, using the yield() when done, and so on.

Tomorrow: Actual testing! Questions/comments welcome etc. See ya!


  1. Can't wait for tomorrow. :D

  2. This is interesting.

  3. Sorry for not doing anything. I've been so tired lately I can barely think. :P

  4. When you talk about a particular aspect, can you reference a line number in the code? That would help me out a lot, thanks!

  5. I cant help but think game editor when I see this, you'd think I would put it to better use

  6. Gotcha, Mike. Will do from now on :P