To us, here and now, it appears thus.

rants, ramblings and other things that make life worth living…

Some Java tips

leave a comment »

For every one who comes from the world of dangling pointers and manual resource management of C++, Java deceptively looks very similar, unless you look under the hood. (I’m pretty much a newbie to Java, who is forced to learn its quirks, primarily due to Hadoop). Most of these tips are written for grad students who’s life cycle can be succinctly paraphrased as “program, collect data, evaluate, rinse, repeat”.

Tip 1. Get a good IDE (read Eclipse)

Unlike the world of C++, where the standard library is essentially small and can for the most part be held in your head, Java has a massive standard library which includes features for everything under the sun. Keeping that in your head is not for the light of heart. Thats where a good IDE like Eclipse comes in, especially the content assist features can make your life a lot easier. Also, the IDE can take a lot of pain out of managing your projects and libraries (because Java has a preferred conventions for writing and using libraries). One more amazing thing with Eclipse is that its got a nifty Java debugger which lets you inspect among other things – the threads currently running in your program.

If you think eclipse is massive bloat or don’t have access to a powerful system for doing your development, (some times I do, where I have to ssh into some machine and change, recompile, rinse, repeat if I am running experiments) then get JDEE (an Emacs plug in).

Tip 2. Get Ant for Building

Ant is a simple Java build system based on XML (yeah.. yeah, you’ve to write XML, stop complaining). In fact, you can use their tutorial build file and start from there. It works with very little modifications, unless you have a special project needs (In which case, Internet is your friend). Say good bye to make files and man up and write some XML.

Tip 3. When you have multiple cores, use Java threads.

Java has threading built in, even though there are some pains with the thread synchronization, Java makes things a bit easier and the best part is on any Linux distribution with NPTL (which is almost every distribution out there), the Java threading model is essentially 1-1, which means you can use all those cores to do the hard work. This is great if you are running experiments under different conditions, in which case, each run of the experiment is essentially independent of others. This usually gives you a performance boost of more that 20-25% (well, but then, I have access to a machine with 8cores and 8gigs of RAM, so YMMV).

Here is a really nice tutorial to get started on Java threads

Tip 4. Learn to use Scanner and Console classes.

For those who mostly work with input output formats from text files Scanner is one of your best friends. It essentially encapsulates the work of reading a line, splitting it into various parts and converting them into your favorite data types. And for all your input requirements learn to use the Console class, which provides a nice interactive console. If you the simple readLine method does not satisfy your needs, Java has a complete regex library, and again, the internet is your friend.

Tip 5. Use jconsole

And for the last tip of the day, learn to use jconsole a monitoring tool for java applications which lets you inspect the amount of memory usage and active threads. If you are using this tool locally, then for the most part there is no configuration required – type jconsole at a command prompt and you should see a window with the list of process that are open and their PIDs. connect to one that you want to monitor and you are ready to track your java process.

Things get a bit tricky with remote process and security issues – so my advice is setup a VPN or use ssh tunneling. More information on using jconsole is available here.

So, thats all for now.

Signing off,
Vishnu Vyas.


Written by vishnuvyas

April 29, 2008 at 4:00 am

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: