Web 2.0

Fashion Over Function

Perhaps you are like me. You heard the term "Web 2.0" and thought it was the most ridiculous catch-phrase you had heard to date. Go ahead, "Google" it. It is the shining stellar example proving that MIS is no longer about information via technology; its all about fashion and status.

How much JavaScript have you written to give the user the feeling of a desktop application on the browser? I, for one, am sick of trying to turn a stateless, thin client, programming paradigm, into a rich stateful fat client experience. Now I am not talking about true e-commerce web sites. Of course, web based businesses, cannot develop, distribute and require the use of a specialized application to make purchases. What I am referring to is your CIO of a typical brick and mortar business being sold on a bunch buzzwords. Think about your last high-tech purchase. How much did it improve your life? And how important was it to get something "cool", something you might stand a reasonable chance of impressing your friends with? Chances are that it did not improve your quality of life all that much (think cellphone) and you probably spent more than was wise for a bunch of bells and whistles. Likewise, why develop a standard client-server application using MFC when you can sprinkle your executive briefing with, AJAX, J2EE, EJB, XML and the all important "platform independent". That last one is real important to most businesses. Employees just do not know what's going to be on their desks come Monday morning. And those kids in the data center are forever swapping out application servers and RDBMS engines. Doesn't everyone just love the ever rotating configuration of hardware and software they have have to use to get their jobs done? What's that? Your still running on a 1GHz Pentium with Windows and Office 2003 and have been for the last 4 years. You mean your databases are still running on Oracle and you haven't had 5 major data migrations in as many years, from Oracle, to DB2, to MySQL, to Informix and back to Oracle?

Sarcasm is a harsh way to make a point but the situation calls for harsh tactics. Things have gotten so out of hand. For instance, I have been building, at a cost of about 1.5 man-years, a web based AJAX and Hibernate fueled speed demon for 4-5 employees to use maybe 4-5 time per fiscal year. We had to use AJAX because of the size of the data sets and the computational costs thereof. The thing is, the more AJAX one writes, the more one begins to realize that you have moved back to the traditional GUI event driven model of the early 1990s but you are trying to use the equivalent of assembly language, hand-crafting all of your own tools.

This "fashionable" aspect of programming is not a recent phenomenon. Take CORBA for example. It was all the rage in the mid '90s. The company I worked for developed products for networked applications and wanted to investigate integrating CORBA into the list of supported paradigms by developing a small application to allow an administrator to control the product, running remotely on one of 4 different operating systems: AIX, HPUX, FTX, and VOS. My manager elected himself the GUI implementor. I had the challenge of writing the various versions of the back-end processes. The only target platform ever mentioned for the GUI was Intel and Windows. The year was 1996. Java was the new darling, the new item no fashionable resume should be without. Given the heterogenous nature of the back-end servers and the homogenous GUI client, what languages were used for each and why? Well, they decided C++ would be used for the multiple platform server processes (for still as yet unknown nefarious reasons) and, for platform independence, Java would be utilized to implement the GUI with its target of a single platform. Why, because a marketing troll wanted to put Java on a glossy brochure and because the manager lacked the strength to do the right thing and was just looking to pad his resume. Now he works for Microsoft. It was hilarious. I watched these guys in meetings when asked why Java was being used for the single target platform client. The hilarious part was the other person's reaction, usually a double take, and the sedate look on the Java propent as though they had just uttered some obvious truth. It was classic "Dilbert" material.

Recently, I had the pleasure of writing a traditional client/server application using Eclipse SWT. Funny, it was just like the JavaScript but without all the hassles of writing industrial strength applications in a scripting language. It was truly striking, the ease with which I could implement the same behavior that might take one or two days to get perfect in a web application. If you have past experience writing MFC or SWT applications but have recently been sequestered to web applications, go back and take your worst web scenario and re-implement it with the traditional GUI technology. I think you will be reminded that there really are different tools for different tasks and that these tools exist for a reason.

DCL Idiom Broken for Java

Okay. It's time we put this one to bed, once and for all. If you have figured out how to make the DCL idiom work for Java, consider yourself among the brightest of the bright in computer science because you are alone. It has been well documented, since the late 90's that the DCL idiom will not work with the current Java Memory Model (JMM, Chap 17 of the spec). There seems to be one main tripping point programmers fall over.

They have no idea how free the compiler (or the u-processor running their code) is to optimize their solutions.

Furthmore, if someone has figured out a good solution, then why is there so much documentation stating that the problem is unsolvable? We are not talking about the average programmer here either. Some of our best scientists have studied this problem for years now, and have been unable to find a solution.

Consider a solution I was offered recently by an architect interviewing me for a programming position.

	public class Singleton {
		private static Singleton _instance;

		public static Singleton getInstance() {
			if(null == _instance) {
				_instance = getPrivateInstance();
			}

			return _instance;  
		}

		private static Singleton getPrivateInstance() {
			if(null == _instance) {
				return new Singleton();
			}
		}
	}
	

Now, I am sure you have caught the compiler problem already. What do we return from the private method when the instance is not null? The compiler is not going to like this. However, let's assume that the author made a simple mistake and in reality would have implemented something that at least compiles.

		private static Singleton getPrivateInstance() {
			if(null == _instance) {
				return new Singleton();
			} else {
				return _instance;
			}
		}
	

What is the compiler is allowed to do with your clever call to a synchronized method. Well, according to Dr. Bill Pugh, it is free to inline your method call. (This is my interpretation since in reality, the problem is at the op-code level and below.)

		public class Singleton {
			private static Singleton _instance;

			public static Singleton getInstance() {
				if(null == _instance) {
					synchronized(Singleton.class) {
						if(null == _instance) {
							_instance = new Singleton();
						} else {
							return _instance;
					}
				}
			}
		}

	

And just like that you are back to the plain-jane version of DCL. Thread A enters, sees that _instance is null, and enters the synchronized block. Thread A continues on and gets to the line _instance = new Singleton();. What happens if the compiler orders the op-codes in such a manner as to cause the assignment of the reference to occur before the creation of the object? In other words, the compiler may allocate the memory and initialize the pointer to that memory before it builds the object that will ultimately occupy that memory. So, the assignment has occured and now thread B enters the getInstance(); method and sees that the _instance reference is not null. In this perfectly legal scenario, thread B will end up with a reference to an object that doesn't exist.

The scariest part of the problem is how it can depend upon the hardware. If we are to be responsible for the differences in each vendor's VM implementation and each deployment's hardware architecture then a lot of programmers are going to be in for a rude awakening.

According to the many experts, the solution is to avoid DCL altogether and use the threading semantics of the class loader or declare the getInstance(); method synchronized. The rationale being that performance improvements have lowered the cost of synchronization by such a degree as to render the topic moot.

References

email exchange on a similar question


----- Original Message ----- 
From: "Bill Pugh"
To: "David McReynolds"
Sent: Thursday, February 19, 2004 3:09 PM
Subject: Re: JSR-133 and DCL


That is very much a standard DCL bug.

Here is one way you could get bit:

* The compiler could inline the call to getPrivateInstance
* The compiler could move the assignment to _instance inside the 
synchronized block
* The compiler could assign the address of the newly allocated object 
before flushing all initialization
   of the newly allocated object to shared memory.





On Feb 19, 2004, at 12:30 PM, David McReynolds wrote:

> Dear Dr. Pugh,
> 
> I hate to flog a dead horse, but was interviewing for a programmer 
> position the other day and someone proposed a rather naive solution to 
> the DCL idiom for java. I don't think it works but lack the detailed 
> knowledge of the JMM to explain why. In XP parlance, the code has a 
> smell about it.
> 
> public class Singleton {
> private Singleton _instance;
> 
> public Singleton getInstance() {
> if(_instance == null ) {
> _instance = getPrivateInstance();
> }
> 
>
> private synchronized Singleton getPrivateInstance() {
> return new Singleton();
> }
> }
> 
> Would you please shed some light on this for me? I've been following 
> the DCL problem for a couple of years now and find it hard to believe 
> that no one else thought of this solution.
> 
> David McReynolds