Archive for category Java

Some tips on java.lang.OutOfMemoryError: unable to create new native thread

Thats right, the dreaded, java.lang.OutOfMemoryError: unable to create new native thread. I fixed one of these the other day. And I will present to you how I did it. The cause for such errors are usually Thread Leaks. A Thread Leak is where some ass hat is creating threads which never die. Another reason for such errors is the limits an Operating System can place on the number of threads per process. If hit this limit, you would ge the same error.
Read the rest of this entry »

JDK Thread Pool Configuration

Introduction

In any non-trivial application of somewhat significant size you will need Executors to background processing or asynchronous processing, task splitting etc. This is where the JDK executors framework comes in.

Read the rest of this entry »

Identifying which Java Thread is consuming most CPU

I didn’t come up with this. I was shown how to do this by an esteemed college at work.

Introduction

Most (if not all) productive systems doing anything important will use more than 1 java thread. And when something goes crazy and your cpu usage is on 100%, it is hard to identify which thread(s) is/are causing this. Or so I thought. Until someone smarter than me showed me how it can be done. And here I will show you how to do it and you too can amaze your family and friends with your geek skillz.

Read the rest of this entry »

An Improved RMI Tutorial with Eclipse

Introduction

There are/have-been heaps of remoting frameworks in java, but RMI being part of the JRE/JDK and therefore having no external dependencies is my personal preference for remoting with java. Its main drawback: the wire protocol is not web-friendly and therefore difficult to go through firewalls (although it is possible). But if used behind the firewall, it makes for an excellent way to do distributed computing using only the JDK (OMG! only the JDK?! no Spring? or JMS?!). It does have several killer features that are found in very few (if any) remoting frameworks: callbacks and remote classloading. In this tutorial, you will see remote classloading. Ever since JRE 5.0, you don’t need to compile stubs (meaning you don’t need an extra compile-time step to get RMI working). I am guessing someone decided to do away with those and use the jdk dynamic proxies.

Read the rest of this entry »

Auto Install the SUN JDK on Linux

Sometimes (yes it does happen), you need to install the JDK from SUN automatically (without having to answer yes/no to the license). Why? For example in a Kick Start script to auto-install the JDK.

And so to do this, here a bash & perl script for this exact purpose. This script works only with the .bin install package that you download from SUN’s website.

#!/bin/bash

jdkBinFile=$1

echo $1

# just spew out the agreement
perl -p -i -e 's/^more/cat/g' $1

# set the 'agreed' value for jdk 1.5
perl -p -i -e 's/\s+agreed=$/agreed=1/g' $1

# set the agree value for jdk 1.6
perl -p -i -e 's/`agree`/yes/g' $1

# do not call the register_jdk method
perl -p -i -e 's/\s+register_JDK/#register_JDK/g' $1

# and now run the installation
bash $1

How to

Create a new script file called autoinstall.sh containing the above script. and run it as follows:

$ cd /install/to/directory
$ autoinstall.sh /my/downloads/jdk-6u16-linux-x64.bin

How to import java projects with eclipse JDT

During the coding of my Bulk Import Plugin, I had the needs to import java projects into a workspace via code. Via Eclipse’s API to be precise. After reading the code of what happens when you import a project, here is the critical bit of code:

Runnable runnable = new Runnable() {
    public void run() {
    try {
        IPath projectDotProjectFile = new Path(pathToMyProjectDir+ "/.project");
            IProjectDescription projectDescription =
                workspace.loadProjectDescription(projectDotProjectFile);
            IProject project = workspace.getRoot().
                getProject(projectDescription.getName());
            JavaCapabilityConfigurationPage.createProject(project, projectDescription
                .getLocationURI(), progressMonitor);
        }
        catch(CoreException e) {
            e.printStackTrace();
        }
    }
};

// and now get the workbench to do the work
final IWorkbench workbench = PlatformUI.getWorkbench();
workbench.getDisplay().syncExec(runnable);

Eclipse Bulk Import V 1.0

Today I released the V 1.0 of my Eclipse Bulk Import plugin. The source is also available at github.

I created this plugin because at work we have too many eclipse projects (over 200). And sometimes the workspaces get corrupt. Using this plugin, I can save the locations of the current projects in my workspace. And I can also import those projects with one-click. Check out the movie of it in action. This plugin saves me quite a bit of time in creating new workspaces (and then importing over 200 projects), and recovering from corrupted workspaces.

Certified “Works on my machine” :) . If you try it out let me know how it goes.

Using Dropbox for Eclipse Projects

I have a bunch of java projects that I like to work on when I have some free time. Only issue is that this “free time” is from different locations, different computers and different Operating Systems. And since I don’t have a lot of “free time”, its hard for me to setup svn, builds etc. I needed a quicker solution: Dropbox

Of course this wont work out of the box. The trick is to make the eclipse projects self contained. In other words, they must not reference any libs/jars/resources outside their own directory. Most self respecting eclipse users would know how to do this. Then the entire project can be synced with Dropbox.

As my project folders will be synced to anywhere I install Dropbox. All I need is a working eclipse and i can start coding. Yay!