Why Linked Lists Are Incompatible With Parallel Programming

December 21st, 2007

Arch Robison (of Intel) writes an interesting piece on his blog about why Linked Lists are incompatible with Parallel Programming, and even gives some reasons as to why to avoid them in your code.

Quite an interesting article, I’ll definitely be looking at some of my code that includes linked lists and looking for some types of alternatives.

Link to Article

kungfuice programming

Java String Comparisons

November 14th, 2007

Today I thought I would take some time to outline basic String comparison in Java. To many newcomers using Java doing comparisons using String can be confusing. Take for example the following piece of code which compares two String literals and two String objects. To newcomers of the Java language the following is confusing. Let’s take some time to outline how this actually works, and what actually makes it confusing.

<font color="#808080">01</font> <font color="#7f0055"><strong>public class </strong></font><font color="#999999">Test </font><font color="#999999">{</font>
<font color="#808080">02</font>     <font color="#7f0055"><strong>public static </strong></font><font color="#7f0055"><strong>void </strong></font><font color="#999999">main</font><font color="#999999">(</font><font color="#999999">String</font><font color="#999999">[] </font><font color="#999999">args</font><font color="#999999">) {</font>
<font color="#808080">03</font>         <font color="#999999">String a = </font><font color="#2a00ff">"abc"</font><font color="#999999">;</font>
<font color="#808080">04</font>         <font color="#999999">String b = </font><font color="#2a00ff">"abc"</font><font color="#999999">;</font>
<font color="#808080">05</font>         <font color="#999999">String c = </font><font color="#7f0055"><strong>new </strong></font><font color="#999999">String</font><font color="#999999">(</font><font color="#2a00ff">"abc"</font><font color="#999999">)</font><font color="#999999">;</font>
<font color="#808080">06</font>         <font color="#999999">String d = </font><font color="#7f0055"><strong>new </strong></font><font color="#999999">String</font><font color="#999999">(</font><font color="#2a00ff">"abc"</font><font color="#999999">)</font><font color="#999999">;</font>
<font color="#808080">07</font>         <font color="#999999">System.out.println</font><font color="#999999">((</font><font color="#999999">a==b</font><font color="#999999">)</font><font color="#999999">;</font><font color="#3f7f5f">//true</font>
<font color="#808080">08</font>         <font color="#999999">System.out.println</font><font color="#999999">((</font><font color="#999999">c==d</font><font color="#999999">))</font><font color="#999999">;</font><font color="#3f7f5f">//false</font>
<font color="#808080">09</font>     <font color="#999999">}</font>
<font color="#808080">10</font> <font color="#999999">}</font></code>

<code><font color="#999999">


First let’s look at the instructions used to generate a String literal in Java. It’s fairly straight forward for anyone who’s seen assembly before. We simply store the literal value using the instruction sets store command.

Instruction used to generate String literal
0: ldc #2; //String abc
2: astore_1

On the other hand there are many more instructions used to generate a String using the new operator. Since we are using the new operator we create a new reference to memory location, duplicate it, load the String literal and invoke a special init function on that new memory. In Java terms we are merely creating a new object and invoking that objects constructor with the parameter “abc”.

Instruction used to generate new String()
6: new #3; //class java/lang/String
9: dup
10: ldc #2; //String abc
12: invokespecial #4; //Method java/lang/String."<init>":(Ljava/lang/String;)V
15: astore_3

So the question still remains, why does a==b return true, and c==d return false? First let’s look at the == operator itself. The == operator works on String object references. If two String references point to the same object in memory, the comparison returns a true result. Otherwise, the comparison returns false, regardless of whether the text has the same character values.

The == operator does not compare actual char data. So we know now that the == operator is looking at comparing two object references, but how can a==b be true?

Well this comes down to the different way Java handles literals. The Java platform creates an internal pool for string literals and constants. String literals and constants that have the exact same char values and length will exist exactly once in the pool.

Subsequent comparisons of String literals and constants with the same char values will always be equal. So you see we could create a million String literals with the value “abc” but Java would only have reference in it’s internal pool to a String literal with value “abc” and length 3.

On the other hand c and d are not String literals they are String objects, and therefore are not created in the Java String literal and constant pool. These objects will each have it’s own reference in memory and therefore will have different values for their references.

Using the == operator to check whether their reference points to the same value will never be true since each occupies it’s own slot in memory.

If we wanted to compare the actual values stored within the String instead of their references, we would simply use the equals method or the compareTo method.

I hope this helps clarify some of the mystery behind how Java handles String and I hope I have helped increase your understanding of String comparisons in Java. Next time we’ll take a closer look at Natural Langauge text comparison in the Java language.

kungfuice Computers, Technology, java, programming

Ubuntu Gutsy

November 1st, 2007

I have been running Ubuntu Gutsy for the last couple of months while it was in Beta and RC stages, but this week marked Gutsy’s full release to the public. I have to say that this is probably one of the best Linux Distro’s I have used to date in terms on out of the box functionality and completeness. It looks like finally someone has listened to many people cries to make Linux more available to regular users, in order to penetrate the Desktop software market.

Although I wouldn’t say Gutsy is a complete success, it definitely has hit the nail on the head in terms on where Linux distributions need to move in the coming future. Being a software developer Linux has always felt comfortable to me. I never really took the time to consider that not everyone in this world wants to spend a day tweaking the kernel, or trying to configure some bleeding edge application. At the same time to me this was one of the things I always loved about Linux, was that it afforded me these luxuries. To me Ubuntu Gutsy finds a happy medium between both of these extremes.

Out of the box Gutsy can be used by any regular user without the need to spend time configuring and setting up the user environment. However Gutsy does not prevent more experienced users from taking this approach. Installing a few tools you can build your own kernel image or compile any bleeding edge software you need. To me this is the real strength in Ubuntu and especially in Gutsy. I find that in my day to day life I don’t always have to be using the the bleeding edge, or do I feel the need to spend hours on end trying to get some new software configured. I am happy with just turning my computer on, firing up my development environment and getting to real work. Don’t get me wrong I still enjoy “playing” around with Linux, but I have found more and more that I expect an OS that just kind of works without too much fuss. I would rather use my time to do my job, or write code then fight to get things working.

I think Gutsy does a good job of achieving this goal for me, since it includes some of the latest bleeding edge software like compiz-fusion, gnome 2.20, open-office 2.3 etc … but it doesn’t require that I spend hours on end getting any of this working, because all of it just works from the start. For example in Feisty I was running beryl, and just before I upgraded I had made the switch to compiz-fusion. Both these pieces of software took me many hours to get configured properly, not to mention that every couple of days they would break because of some new change to the SVN code base. This was acceptable for me since I knew ahead of time that both pieces of software were bleeding edge and would have their issues, but when you need to print a simple PDF and can’t get X to come up it can be a little frustrating.

Gutsy has done a good job of integrating these new bleeding edge pieces of software but at the same time running versions that are relatively stable and offer a good user experience.

A supposed great new feature of Gutsy is it’s automatic printer configuration. Gutsy will find printers connected to your computer and on your Network and automatically configure them to “work”. Well I have yet to get this feature to “work” properly, and since my printer worked just fine in Feisty I find this to be extremely frustrating. Although I don’t physically have a printer connected to my machine I do have a laser printer shared from a Windows XP box in our basement. It’s a simple Samba based printer share that worked flawlessly in Feisty and on all my other machines, except for Gutsy. It simply won’t print anything, it won’t even spit out garbage, since I’ve been so busy with school as of late I haven’t really gotten a chance to sit down and figure out why it’s not working anymore.

Other then this oddity though, Gutsy has been a great upgrade for me, and I don’t regret moving up to this release. I recommend Gutsy for anyone who wants to use Linux, whether you’ve been using it for years or simply want to make the plunge to the Linux dark side. I promise you, you won’t be disappointed.

Look forward to some configuration and tweaking articles I’m putting together, to really maximize how your gutsy install performs.

kungfuice Technology, Ubuntu

Compiz Fusion Window Snapping

October 30th, 2007

Well I have been using compiz fusion and beryl for some time now, but ever since I made the switch to compiz fusion I have noticed a very annoying “bug”.  When you try to move a window anywhere near the top of the screen it is repositioned to the very bottom, or simply sits at the top of the screen wobbling incessantly.

I tried a couple of different things to try and get it to stop the first being to disable window snapping, but for some reason that just didn’t work. Luckily trusty google was to the rescue and has provided me with a great fix for this silly problem.

I wish I could take full credit for this but I really have to thank TechBlog for this one.  The issue really comes down to a property of the wobbly window plugin called “Snap Inverted”.  Disabling this property gets rid of this annoying behaviour and makes compiz fusion work with windows properly. The steps for fixing this are rather simple, so without further adu let’s get to it.  These steps work with Ubuntu Gutsy, but should work fine with any Distro runing compiz fusion and having the compiz-settings-manager installed.

  1. Goto System -> Preferences -> Advanced Desktop Effects Settings.
  2. Scroll down to the “Effects” Section and Click the “Wobbly Windows” plugin.
  3. Under the “General” tab you will see as your first option “Snap Inverted” simply uncheck the checkbox beside it to disable.
  4. You are now done, close down the Compiz Setting Manager and enjoy.

I hope this helps anyone getting really annoyed with this behaviour.  Hopefully compiz will disable this property by default in upcoming releases.

kungfuice Linux, Technology, Ubuntu

New Job

September 24th, 2007

I guess I should mention in regards to my other post that I have gotten a new job. I have taken an offer to work at Research In Motion full time as of January. I can’t wait to go back, since I really enjoyed myself this summer. I can’t wait for the challenges I know I will face working there, and I can’t wait to get back on my team.

So I guess as of January I will officially be a Waterloo resident, so if anyone knows of any good places for me to live hit me up, because I’m looking for a place! :D

I guess back to my suffering with school work

kungfuice General