Just another weblog

Java Threads CPU Usage

Posted by damuchinni on February 25, 2009

It’s a known fact to every Java developer that not being careful on making use of threads on your application would most probably make it your greatest foe instead of making things better for you. Honestly speaking, I tried to stay away with threads as much as I could. Not because, I am not efficient with it but rather, I am just playing safe when it comes to the problems it might pose on my application, especially in terms of memory leaks.

Sad to say, when you are working with a GUI (Graphical User Interface) based application, it’s less likely that you won’t be using threads. This is due to the reason, that there are a lot of methods that would be best called in the background rather than having the user wait for its completion and make your application hang during this time. One good example would be data population on JTables wherein it won’t really look nice for the user to wait until 500,000 rows of data are retrieved and displayed. Wouldn’t it be nice if things would happen in the background and your application’s UI would still be responsive during this time?

Just recently, I had to work on a Swing/AWT based application and there are some instances wherein I needed to use java threads. Our API makes use of custom Swing/AWT objects and for some reasons, our custom JTextField don’t respond to text input listeners. Instead of trying to change the API, I decided to just think of some workaround so as to prevent introducing new bugs on other components, considering that we are already on a tight deadline for the customer delivery. To make the long story short, I had to create a way in which drop-down menus would be disabled when the user inputs a text inside the TextField and enable the said drop-down menus when no text exists inside the TextField. It’s pretty simple actually, if only our TextField component would listen to text input listeners, but it’s not the case. So what I did was to create a thread that checks the TextField if it contains text or not, and then disable or enable the drop-down menus accordingly. This was the first approach I did:

private boolean monitor = true;
class TextFieldMonitorThread extends Thread {
public void run() {
while (monitor) {


This was obviously an unending loop which I thought woud handle the TextField monitoring well in real-time, but I was wrong. Even though it didn’t pose any memory leaks, it made the CPU usage spike above normal taking around 70% – 80% of the CPU resources.

So what I did was just make use of the TimerThread class so as to make things much easier in terms of CPU usage. This basically creates a background thread that checks the TextField every second.

class TextFieldMonitorTask extends TimerTask {
public void run() {


TextFieldMonitorTask fmt = new TextFieldMonitorTask();
Timer timer = new Timer();
timer.scheduleAtFixedRate(fmt, 0, 1000);

I’m glad this resolved the problem on CPU usage, even though there might be some slight delay before the drop-down menus were disable or enabled considering the 1 second check interval. Then again this is a pretty good trade off rather than slow down the whole application or even the whole server all together.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: