Latest Publications

Hidden Java 7 Features – System and Process CPU Load Monitoring

A couple of weeks ago I wrote a summary of 7 new cool features in Java 7. Others have written great blog posts about them, so I was thinking it might be useful for me to focus on lesser known or “Hidden Java 7 Features”. In the next few weeks, I’ll try to explore and write a series of blog posts about them.

System and Process CPU Load Monitoring

Java 7 provides a new MBean that finally allows Java developers to access overall System and current process CPU load monitoring. This feature is not really that hidden as it’s listed in the what’s new page for JDK 7, but I haven’t really noticed a lot of talk about it and I certainly overlooked it initially.

The interface com.sun.management.OperatingSystemMXBean, has two new methods that easily get this information; getSystemCpuLoad() and getProcessCpuLoad(). Both methods return the load as a double value in the [0.0,1.0] interval, with 0.0 representing no load to 1.0 representing 100% CPU load for the whole system or the current JVM process respectively.

It’s pretty easy to get access to this MBean;

import com.sun.management.OperatingSystemMXBean;
...
OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(
                OperatingSystemMXBean.class);
// What % CPU load this current JVM is taking, from 0.0-1.0
System.out.println(osBean.getProcessCpuLoad());

// What % load the overall system is at, from 0.0-1.0
System.out.println(osBean.getSystemCpuLoad());

Wasn’t this already available?
Java 6 introduced a new method in java.lang.management.OperatingSystemMXBean called getSystemLoadAverage(), which could get the CPU average for the overall system (there wasn’t a mechanism for the JVM/current process load). However this doesn’t seem to work on Windows and would just return a -1 (tried this with Java 7 in Windows 7 and that is still the case).

Other ways of doing this prior to Java 7 would include Java APIs implemented via native libraries like SIGAR, which are good alternatives but obviously not part of the standard Java distribution, or may not support most platforms.

Now the bad news
If you notice in my code I use com.sun.management.OperatingSystemMXBean, not java.lang.management.OperatingSystemMXBean which means the methods getSystemCpuLoad() and getProcessCpuLoad() are not part of the official, supported public interface of the Java Platform. Oracle actually discourages the use of these packages, since they could be removed in future releases or may not be available in all JVM implementations.

I’m not 100% sure why these methods were only added to the com.sun version of OperatingSystemMXBean. Initially I thought it was because Oracle didn’t want to add a method to a public interface (which would break any code implementing that interface), however Java 6 added getSystemLoadAverage(). If anybody has any insight on this, please let me know.

There are ways to use these new methods without having a hard dependency to these com.sun classes (for example via reflection, or using JMX conventions), I may cover these in a future blog post or update to this one.

Demo, JCPUMon
Finally, I wanted to do a simple UI that would use this feature to have a little widget to monitor system CPU and current process load graphically. I call it “JCPMon”, it can be launched as a standalone Java application, run inside an application you want to monitor or just used as a Swing component in a Java UI.

The green part of the graph represents the overall system CPU load, while the yellow part represents the current process load, in this case CPU load imposed by the GUI (I did a lot of resizing to get a few yellow bars for this image :-) ). If you want to monitor a Java application you already have, you can easily do so by calling;

// This method won't block, so you can call it at the start of your program
com.sellmic.apps.monitoring.jcpumon.Main(null);

// And then continue doing anything else ...

Of course an improvement would be to have the ability to connect to an external process via JMX (remote or local).

You can launch this app via webstart, .You’ll need Java 7 to be installed.

This simple GUI uses this new Java 7 capability and also has some extras to configure the look of the UI (squares or circles, size, how often to update, always on top, full screen). If anybody is interested in the code for this let me know and I can setup a page to host it.

UPDATE: JNLP URL was wrong, if you tried webstart before and it failed please try again. Sorry about that!

7 new cool features in Java 7

The evolution of the Java language and VM continues! Mark Reinhold (Chief Architect of the Java Platform Group) announced yesterday that the first release candidate for Java 7 is available for download, he confirms that the final released date is planned for July 28.

For a good overview of the new features and what’s coming next in Java 8, I recommend this video on the Oracle Media Network, which features Adam Messinger, Mark Reinhold, John Rose and Joe Darcy. I think Mark sums up Java 7 very well when describing it as an evolutionary release, with good number “smaller” changes that make for a great update to the language and platform.

I had a chance to play a bit with the release candidate (made easier by the Netbeans 7  JDK 7 support!), and decided to list 7 features (full list is here) I’m particularly excited about. Here they are;

1. Strings in switch Statements (doc)

Did you know previous to Java 7 you could only do a switch on char, byte, short, int, Character, Byte, Short, Integer, or an enum type (spec)? Java 7 adds Strings making the switch instruction much friendlier to String inputs. The alternative before was to do with with if/else if/else statements paired with a bunch of String.equals() calls. The result is much cleaner and compact code.

    public void testStringSwitch(String direction) {
        switch (direction) {
             case "up":
                 y--;
             break;

             case "down":
                 y++;
             break;

             case "left":
                 x--;
             break;

             case "right":
                 x++;
             break;

            default:
                System.out.println("Invalid direction!");
            break;
        }
    }

2. Type Inference for Generic Instance Creation (doc)

Previously when using generics you had to specify the type twice, in the declaration and the constructor;

List<String> strings = new ArrayList<String>();

In Java 7, you just use the diamond operator without the type;

List<String> strings = new ArrayList<>();

If the compiler can infer the type arguments from the context, it does all the work for you. Note that you have always been able do a “new ArrayList()” without the type, but this results in an unchecked conversion warning.

Type inference becomes even more useful for more complex cases;

// Pre-Java 7
// Map<String,Map<String,int>>m=new HashMap<String, Map<String,int>>();

// Java 7
Map<String, Map<String, int>> m = new HashMap<>();

3. Multiple Exception Handling Syntax (doc)

Tired of repetitive error handling code in “exception happy” APIs like java.io and java.lang.reflect?

try {
    Class a = Class.forName("wrongClassName");
    Object instance = a.newInstance();
} catch (ClassNotFoundException ex) {
    System.out.println("Failed to create instance");
} catch (IllegalAccessException ex) {
    System.out.println("Failed to create instance");
} catch (InstantiationException ex) {
   System.out.println("Failed to create instance");
}

When the exception handling is basically the same, the improved catch operator now supports multiple exceptions in a single statement separated by “|”.

try {
    Class a = Class.forName("wrongClassName");
    Object instance = a.newInstance();
} catch (ClassNotFoundException | IllegalAccessException |
   InstantiationException ex) {
   System.out.println("Failed to create instance");
}

Sometimes developers use a “catch (Exception ex) to achieve a similar result, but that’s a dangerous idea because it makes code catch exceptions it can’t handle and instead should bubble up (IllegalArgumentException, OutOfMemoryError, etc.).
(more…)

Facebook Friend Lists suck when compared to Google+ Circles

Early reviews of Google+ are in and they’re generally positive. Obviously, not everybody love Google’s latest social network experiment, but I’d like to debunk one piece of criticism that keeps being repeated over and over. And that is, that the Google+ Circles feature is basically the same thing as Facebook’s Friend Lists.

Facebook Friend Lists lets users group friends under different lists; family, co-workers, etc., so that you can share things with subsets of your overall Facebook friends. As in real life, you don’t share everything with all the people you know, and with the Friend Lists feature you can emulate that. Sounds very similar to Circles right?

The problem with Friend Lists is the poor usability of this feature. Unlike Circles, you get the feeling that this feature was tacked on, and that it’s not a central component of the service. Creating and assigning people to Circles in Google+ is a lot easier and friendlier than managing Friend Lists, just look at this video from Google which gives you a good overview of how the Google interface handles this.

The main problem with Friend Lists is that once setup they’re a pain to use, which is probably why a lot of Facebook users either don’t know about this feature or don’t bother to use it.

Let’s try to share a message only intended for our family;

First non-obvious thing you have to do is click on the padlock icon. This icon gives you 4 choices; “Everyone“, “Friends of Friends“, “Friends Only” and “Customize“. Unlike Circles, it doesn’t list your Circles of friends or in this case your Friend Lists. What you have to do here is select the “Customize” option which opens up the “Custom Privacy” dialog.

The “Custom Privacy” dialog now gives us another 4 choices; “Friends of Friends“, “Friends Only“, “Specific People…“, “Only Me“. Again, Facebook decides not to show you your Friend Lists, and to select one you have to chose the “Specific People…” option. Now, in the previous screen, we already told the interface we don’t want to send a message to “Friends Only” or “Friends of Friends”, why is it showing us these options again? Bad UI design.

There’s also the strange option to only share an item with yourself (“Only Me“), I guess that’s usefull to save things and share them later. But instead of listing this, I’d rather see my family Friend List.

To find our desired list, again we select “Specific People… (1), which shows an empty text entry field. As we start typing “f” for family (2), Facebook is nice enough to show us friends with first or last names that contain the letter “f”. Note that I have so many friends with that letter that in none of the choices my family Friend List shows up. After typing a bit more, “fa” (3), we still see other friends but at the bottom of the choices we finally see our family Friend List. Finally we select it (4) and our family list is added to the field.

Another bad design choice is how Friend Lists are mixed in with regular (singular) friends, at the very least it would be nice to get a visual hint that something is a Friend List vs a singular specific friend.

We then click “Save Setting” at the bottom right of the dialog and go back to our Facebook wall. Obviously, the last step is to press the “share” button, unfortunately Facebook doesn’t make it clear what Friend List or group of friends I’m sharing with. To see that, you need to click again on the padlock icon which instead of indicating what Friend List is selected, it shows a cryptic “Custom edit” selected item on the padlock popup menu. Not clearly showing who we’re sharing with makes it easy to share the wrong thing with the wrong group of people by accident. Again, bad UI design.

And that’s how Friend Lists are used in Facebook. The feature is hidden via the padlock icon, the option is not clear (“Customize“), selection is cumbersome, and Facebook does its best to hide away Friend Lists from the user. It’s almost as if the UI is designed on purpose to discourage the use of this feature.

In contrast, in the Google+ interface, you simply type the text you want to share (or image, video, etc.) and then click on the “Share” button;

From right there you can select with Circle of friends you want to share with. The Circle can be typed in or selected from the list, if it’s not shown in the list simply clicking “more …” will show the rest of the Circles. Unlike Facebook, Circles are not hidden but featured front and center. In this case 2 clicks allow me to quickly select who I want to share with.

Another key usability item to note, once a Circle is selected, Google+ makes it very clear who you are sharing with. In this example we can see the family Circle highlighted in blue and with a Circle icon to denote it represents a group of people. Singular friends added to the group, will also be shown highlighted in blue but without the Circle icon to differentiate them with Circles.This is much better than clicking on a padlock and trying to figure out who “Custom edit” is.

Simply put, when contrasting both approaches we note that;

  1. Google has spent a lot more effort on usability in their User Interface design; minimizing clicks, removing the need for additional dialogs, preventing users from sharing information with an unintended Circle, etc.
  2. Circles is a key and central feature of Google+, whereas Friend Lists seems more like a feature added as an afterthought, rather than a fundamental aspect of how people share information with different groups of friends.

It is clear that Google+ Circles are friendlier and easier to use than Facebook Lists. On this one central aspect of sharing, it seems Google has done their homework while Facebook really needs to evaluate and reinvent their approach to usability.

Library Troll

Book troll spotted at local library

iTunes AppleSyncNotifier error, how to fix

I kept getting this annoying error on Windows start-up every time I would login (running Windows 7, 64 bit).

Dialog: AppleSyncNotifier.exe – Entry Point Not Found. Message: The procedure entry point kCFStreamSocketSecurityLevelNegotiatedSSL could not be located in the dynamic link library CFNetwork.dll.

First thing to do is go to Control Panel->System->Advanced System Settings->System Properties (Dialog)->Advanced (Tab)->Environment Variables. You should see a dialog like this;

Now check your Path (PATH) variable by selecting the Path list item and clicking on the Edit button. You’ll see a lot of paths there, now the problem is that one of the paths in that field is making it so the AppleSyncNotifier loads the wrong library file (CFNetwork.dll)! In my case I have CA (Computer Associates) Unicenter software installed which had this path included;

C:\Program Files (x86)\CA\DSM\bin; … (full PATH truncated)

This makes iTunes load the CA version of CFNetwork.dll and generating this error.

Now if you get this error and don’t have CA or this path, please check all the paths in your PATH variable, and it is very likely that one of them from another program is loading the wrong CFNetwork.dll (do a search for CFNetwork.dll in your C:\Program Files folder).

So to fix this, let’s add the Apple path to the correct CFNetwork.dll, by prepending the PATH variable to point to it. In my case I added this path before the CA one;

C:\Program Files (x86)\Common Files\Apple\Apple Application Support; (remember to add “;” at the end before the paths that follow).

Click OK, on this, the Environment Variables and the System Properties dialogs and that’s it. Next time you reboot you shouldn’t see this error anymore.

Serious Java bug when parsing 2.2250738585072012e-308

Noticed a good post on this bug here via the Java Posse Google Group, if you parse the double “2.2250738585072012e-308″ it will hang a Thread, both at runtime (via parseDouble) or at compile time (just by defining referencing that number as a literal).

Examples (from the post at exploringbinary.com);

class runhang {
public static void main(String[] args) {
  System.out.println("Test:");
  double d = Double.parseDouble("2.2250738585072012e-308");
  System.out.println("Value: " + d);
 }
}

And then at compile time;

class compilehang {
public static void main(String[] args) {
  double d = 2.2250738585072012e-308;
  System.out.println("Value: " + d);
 }
}

See that blog post for a detailed analysis, it has links to the offending file in question (FloatingDecimal.java) and a pretty long comment list that is worth reading. Everybody is still trying to access what this impacts, and it seems to impact JVM based languages of course, and all platforms. I did notice on the Java Posse Group people saying that Android and Harmony are not affected (since they’re different implementations of course).

Impact of the bug? The most obvious one I can see is you can hang a thread on any JVM based software that accepts an input string that will be eventually parsed as a double. So for example a servlet on tomcat, you can eventually hang all HTTP threads and exhaust it’s thread pool to the point were it won’t be able to handle any more incoming request unless you restart the server. Ouch!

The bug will hang a thread, not the whole JVM (I wanted to clarify this because some people are saying it hangs the JVM, but it’s really the thread that is processing the call).

class hangthread {

    public static void main(String[] args) {

    Thread t1 = new Thread() {
        @Override
        public void run() {hangme();}};

    Thread t2 = new Thread() {
        @Override
        public void run() {hangme();}};

    Thread t3 = new Thread() {
        @Override
        public void run() {amialive();}};

        t3.start();
        t2.start();
        t1.start();
    }

    public static void amialive()
    {
        while (true){ System.out.println("I'm alive ...");
        try {Thread.sleep(1000);} catch (Exception e){}
        }
    }

    public static void hangme()
    {
        System.out.println("Test:" + Thread.currentThread().getName());
        double d = Double.parseDouble("2.2250738585072012e-308");
        System.out.println("Value: " + d);
    }
}

Seems this bug was originally reported to Sun on 04-MAR-2001(infinite loop while parsing double literal) !!!

 

WP SlimStat