Thursday, November 16, 2006

Thread pooling in Java 5

Only recently I began learning (and working with) Java 5's new features. When they announced the most emblematic new features there was an immediate reaction to them. Some people loved the new features, others hated it. To be true, I liked some things (like enumerations and generics) but never cared much about auto-boxing or varargs. However, I'm not going to talk about these popular features that almost everyone has heard of. It seems there's plenty more to know about Java 5. Today I discovered a fantastic (first impressions) and powerful new feature: java.util.concurrent package. This package provides several utilities to handle threads and locks, simplifying the whole process. For now I only tried one thing: creating thread pools. This is something that comes in handy very often and it's now very simple to do.

First, you have the usual Runnable that implements whatever you want the thread to do. In this simple example it just prints "hello".


public class WorkerThread implements Runnable {
public void run() {
System.out.println("hello");
}
}

Now, let's imagine I want to create 300 WorkerThread s, but allowing only 5 threads simultaneously. So, a thread pool has to be created with size 5. Then I submit the 300 tasks to the pool, which only executes 5 at a time, queuing the rest. As one thread finishes, the next one in the queue starts.

ExecutorService pool = Executors.newFixedThreadPool(5);
for(int i=0; i<300; i++){
pool.submit(new WorkerThread());
}

As simple as that. But there's a lot more to learn. For example, there's a new interface Callable, that you can use instead of Runnable. Callable allows the executing method to return arbitrary Objects and throw exceptions, something you couldn't do with Runnable's run() method. Look here for more information about this and other cool enhancements that didn't get the overall attention they deserve.

Tuesday, November 07, 2006

LCD Monitor guide

A few days ago my LCD monitor started making some strange noises. I knew that it wouldn't last long. It still works, but barely (the image goes black from time to time). Although I might try to get it fixed, I immediately started searching for a new one. Normally I like to learn about all the little differences and rate the available choices. However, there's just one little problem : there's hundreds of different monitors available!! No I didn't quit, but instead of collecting all the information I started eliminating most of them based on a few characteristics. So here's what I found out.

First and most important of all: the budget. Because I didn't want to buy a very expensive monitor, I think I reduced my choices to half :D

Then there's the size. I own a 17" and I probably will buy another 17", but I still haven't discarded completely an 19" monitor. I won't get a better resolution with a 19", it's the same 1280x1024 that I get with a 17". Everything just looks a little bit bigger (mostly the fonts). I think it's not better or worse. It depends on the distance to the monitor. I read somewhere the distance from the eyes to the monitor should be twice the diagonal. Basically it comes down to which you feel more comfortable with. And I feel comfortable with the 17". Don't think it's the price...I surprisingly found a few 19" monitors cheaper than most 17". There's also the widescreen's, which may be better for some situations, like watching DVD's. But I'm still not sure about those...never worked with one so far.

At this point, the rules I outlined already exclude lots of monitors, but still there's too many and I'm lazy enough to just pick a few well-known brands instead of going trough all of them! Also, it helps if the monitor you want is available at your local stores (unless you want to buy online) and that should reduce a few more.

LCD Monitors have a lot of specifications, but not everything is important:

  • Contrast ratio - The contrast ratio is an important factor, but different vendors calculate this value in a different way. So, it's not a reliable parameter. There are lots of 500:1, 700:1...and then there are a few 2000:1. Clearly this last one was measured differently. I just established a minimum here (500:1) and then pushed this parameter to the bottom of the decision-making process.
  • Interface - every monitor has an analog input, and some have the better digital input (DVI). I'm not sure how much better is the digital input (some report better image quality, due to faster image render, because there's no analog/digital conversion) but I chose only monitors with DVI input.
  • Brightness - this is almost everytime 250 cd/m2, so I completely ignored this.
  • viewing angle - most monitors have 160/160, so I excluded everything worse than that. I like to be able to look at the monitor from different angles, and lower values don't allow good viewing.
  • response time - I've seen values from 2ms to 12ms. From what I've found 12ms is very good and anything below that isn't that noticeable. Of course there are monitors with response times greater than 12ms (which I ruled out) but most of the new ones already have at least 12ms.
  • dot pitch - this almost never varies, so I ignored this.
  • power consumption - you could just ignore this, but if you care about the environment (and your bills) you could choose a monitor with better consumption. The best I saw was 30W when "on" and 1W in "standby".
  • base - it's important to have the monitor well positioned, so the more options it gives you, the better. Possible adjustments are: rotate, tilt, height adjustment and pivot. Almost every monitor has tilt adjustment. Rotate it's also very common. Height adjustment is the most important to me, but it's not so common. Pivot is becoming more common, but I have no use for it.
  • extras - some monitor have lot of extras, like speakers, USB ports, etc. I prefer not having to pay for these useless extras.

You might be asking: "so which one did you choose ?" :) Well, I have a small list but haven't made up my mind yet. And I also don't want to spoil you this wonderful experience of choosing monitors :P