Sunday, August 13, 2006

Remembering the Tiger (Java 5.0) - Part 1

Before we move onto Java Mustang, let us remember the Tiger! The focus of this part is Multi-threaded programming (usually done in server side applications) with the new features of the Tiger (Java 5.0).

New Multi Threading Features of Tiger Java 5.0

1. New Objects
- StringBuilder Vs. StringBuffer
- Atomic Classes (AtomicInteger, AtomicLong, AtomicBoolean, AtomicReference)

- Concurrent Collection API
- Concurrent Collection - Blocking Queues

2. New Features
- Covariant Return
- Generics


3. Some Optimization Techniques
- Usage of final keyword
- Avoid Enumeration or an Iterator
- Usage of Stack variables when possible
- Minimize Synchronization


4. Threads
- Synchronization Classes (Locks, Barri
er, Semaphore, Countdown Latch, Exchanger & Reader/Writer Locks)
- Thread Pool
- Exception Handling

1. New Objects

StringBuilder

Java Architects / Developers who create high performance multi threaded Java Applications are aware of the cost of creating objects and synchronization
costs. Following is a simple example of unwanted object creation.

String host = new String("Web Server");
String ip = new String("10.10.10.1");
int port = 80;

String displayValue1 = host + " (" + ip + ":" + port + ")";


System.out.println(displayValue1); // Web Server (10.10.10.1:80)

/**
* java.lang.StringBuffer
* The default capacity of StringBuffer is 16 bytes. When the capacity reaches the limit then
* StringBuffer creates a new array (char) doubling the previous size and copies the data to
* the new array. Old array gets discarded at the time garbage collection. Key to optimization
* is creating a StringBuffer with required capacity.

*/

StringBuffer sbuf = new StringBuffer(64);
sbuf.append(host).append(" (").append(ip).append(":").app
end(port).append(")");

String displayValue2 = sbuf.toString();

System.out.println(displayValue2); // Web Server (10.10.10.1:80)


/**
* java.lang.StringBuilder()
* StringBuffer is thread safe. However if your data set is thread local then use StringBuilder
* instead. Avoiding unwanted synchronization improves the performance further.

*/

StringBuilder sbuild = new StringBuilder(64);
sbuild.append(host).append(" (").append(ip).append(":").append(port).append(")");

String displayValue3 = sbuild.toString();

System.out.println(displayValue3); // Web Server (10.10.10.1:80)

Atomic Classes (java.util.concurrent.atomic.*)


From the performance perspective, one of the key objectives of the thread safe programming is shrinking the synchronization scope. When ever we use primitives which need to be shared across the threads we make the primitives volatile or synchronize that section of the code.

Volatile variables can be safely used only for a single read and write operations. It cannot be used for an operation like ++ / -- because it contains multiple operations like read, modify and write while the new Atomic Classes can be used for similar functions atomically. However, these new Atomic Classes require hardware support to make the multiple operations (like read, modify and write) atomic. The hardware guarantees that these operations are atomic.

Compare and Swap (CAS)

Compare and Swap is a special CPU instruction (used in multi processor systems) which takes three values: A memory location (L), expected old value (O) and a new value (N).
The Processor will atomically compare the contents of a memory location to the expected (old) value and if they are same, then it modifies that content to a given new value (N) other wise it will do nothing. However, in either case it returns the value that was at the location prior to the CAS instruction. (Some flavors of CAS will instead simply return whether or not CAS succeeded.)

Intel processors implements CAS by the cmpxchg family of instructions, while MIPS and PowerPC have a pair of instructions. MIPS has “load linked” and “store conditional” while PowerPC has “load and reserve” and “store conditional”.

- AtomicBoolean
- AtomicInteger

- AtomicLong
- AtomicReference


As part of Tiger (Java 5.0) release java introduced Atomic classes like AtomicInteger, AtomicLong, AtomicBoolean, AtomicReference which handle integers, long, Booleans and Objects respectively. These classes allow multiple operations like read, modify and write in a single atomic operation. Most of the functionality in the ReentrantLock class (Mutex) uses Atomic classes for the implementation. Atomic classes can be even used to avoid synchronization at the cost of complex code and design. This functionality is implemented at the native level.

// Testing AtomicInteger Class

AtomicInteger ai = new AtomicInteger(10); // Initialized with value 10

System.out.println("Value Get-And-Set(2) = "+ai.getAndSet(2)); // Displays 10
System.out.println("Value Get() = "+ai.get()); // Displays 2


In the above example getAndSet() method returns the original value (10) before setting the new value as 2. However, as these operations are atomic this doesn’t require any synchronization locks. Similar methods are incrementAndGet(), getAndIncrement(),
decrementAndGet(), getAndDecrement(), addAndGet(), getAndAdd().

The conditional modifier methods compareAndSet() and weakCompareAndSet() takes two arguments – the expected value and the new value. If the current value is not the expected value then new value is discarded and the method returns false else the new value is set and the method returns true. The weakCompareAndSet() returns false then the variable is not set with new value, however, that doesn’t mean that the existing value is not the expected value.

Atomic package also supports Arrays, AtomicIntegerArray, AtomicLongArray and AtomicReferenceArray. You can’t modify the whole array atomically; however, you can modify one indexed variable at a time. There are no implementations for an array of Booleans as this can be achieved by using AtomicIntegerArray.

Other classes in the package to complete this brief overview of the Atomic package are AtomicMarkableReference and AtomicStampedReference. The former maintains an object reference along with a Boolean that can be updated atomically while the latter maintains the object reference with an integer “stamp” that can be updated atomically.


Concurrent Collection API (java.util.concurrent.*)

-
ConcurrentHashMap
-
CopyOnWriteArrayList
- CopyOnWriteArraySet
- ConcurrentLinkedQueue

All the above classes are nearly Thread safe. ConcurrentHashMap uses less synchronization than the Hashtable. CopyOnWriteArray List and Set provides unsynchronized iterator access, while ConcurrentLinkedQueue an unbounded thread safe non blocking FIFO queue.

Concurrent Collection API – Blocking Queues (java.util.concurrent.*)

-
ArrayBlockingQueue (Bounded FIFO Queue)
- DelayQueue (UnBounded Queue with time based order)
- LinkedBlockingQueue (Bounded / UnBounded FIFO Queue)
- PriorityBlockingQueue
-
SynchronousQueue (Bounded FIFO Queue)

All the above classes are thread safe and notifies the thread when the content changes. It implements BlockingQueue interface that allows the threads to wait either if the queue is full (when the thread try to store data) or if the queue is empty (when the thread tries to retrieve data).

2. New Features

Covariant Return

Java 5.0 introduced the concept of covariant return where you can override the return type of a method in a class where the return type is the subclass of the overridden method (of the super class return type). Let me show that using a sample code. The following code shows two vehicle types Car and Van (Van extends from Car).

The following code shows two Vehicle Factory types NormalVehicleFactory and FamilyVehicleFactory (extends from NormalVehicleFactory). However, what’s odd is the getVehicle() method call. The FamilyVehicleFactory overrides the getVehicle() method and return a sub class of Car (which is Van).



3. Some Optimization Techniques

Similar to the usage of StringBuilder and other new Collection API’s, as an Architect / Developer you need to keep in mind on performance optimization techniques to build a scalable performance focused application. This part will be concluded in the next few days…..
10. Sun Microsystem – Untangling the Threads
Books

1. Addison-Wesley (May 9, 2006) –
Java Concurrency in Practice By Brian Goetz
2. O’Reilly (Sept 13, 2004) – Java Threads (3rd Edition) By Scot Oaks & Henry Wong
3. O’Reilly (June 25, 2004)– Java 5.0 Tiger A Developers Notebook By B McLaughlin, D Flanagan
4. Addison-Wesley (Feb 4, 2000) –
Practical Java By Peter Haggar

(Part 2 of this series will focus on Threads and new Synchronization techniques...)

Monday, April 24, 2006

Rich Internet Applications

Rich Internet Applications are the next wave in User Interfaces (Google Gmail, Google RSS Reader, Google Finance etc). AJAX (Asynchronous JavaScript and XML) is a key technology in this direction. RIA’s are designed to deliver 8A’s of software simplicity. 8A’s is Bill Gates old information at your finger tips or IBM SAA (System Application Architecture). According to Gartner (Article: RIA’s are the next evolution of the web by Mark, Ray, Gene) at least 60% of the application development in 2010 will include RIA technologies and 25% in that will primarily on RIA.

Applications Able to deliver Access to Anyone Authorized Anytime, Anyplace on Any Device.

Evolution of the Application User Interface

From the command line user interfaces in the 80’s to the Client Server based Graphical User interfaces in the early 90’s to the Web based applications in the late 90s and currently to the Rich Internet Applications which has the thin client framework with features of the Thick Clients (Client Server GUI).


Main Frame Text UI > Client Server Apps GUI > Web Apps (Web Pages) > RIA


AJAX technologies are not new. All the components in Ajax technologies existed from 1998 onwards. The term AJAX was born in 2005 and made it popular by Google applications like Google Suggest, Google Maps and GMail. Even Microsoft through its Atlas project is pushing the AJAX developments. The term AJAX was coined by Jesse James Garret of Adaptive Path. The Following diagram shows the comparison between a traditional Web Application with an AJAX based web application. In the traditional model all the request are based on a user action and every request require a new web page to be returned by the web server. In the new model initial request is based on the user action and then lot of asynchronous request(s) were made from the RIA (the client) to the server to give a traditional thick client user experience.

New Architecture Model – Four Layers of App Stack (FLAST)

This New Architecture model is similar to the IP Protocol Stack. Following diagram illustrates the details of each layer. Client Layer focuses on the Browser with AJAX Engines / Macromedia Flash Engines etc. This layer gives a Rich Interface to the end user similar to Client side heavy GUI applications. This layer transparently talks to the back end presentation layer build using JSP, ASP, PHP or Servlets. Asynchronous calls where made from the Client layer to the Presentation Layer. Presentation Layer talks to the Business Layer for handling the Business functions which in turn talks to the infrastructure layer to store and retrieve the data.


Features of AJAX
  • GUI Widgets Rich Applications in the Web model without the support Java or Flash compared to the static or dynamic HTML pages.
  • It uses existing technologies like JavaScript, XML. The backend can be written in PHP, Perl, Java, C# etc. This helps to avoid any new technology learning curve for the developers. So, the developers can use their existing skill set to develop highly interactive AJAX web based applications.
  • AJAX applications run on any browser which supports JavaScript (and enabled). This makes a wide Browser support under all platforms at any time.
  • These applications even run on low bandwidth environments.
  • RIA is deployed without a separate installation process.
Key points to consider and aware of when you build an application using AJAX.
  • AJAX apps are not traditional ‘Web Page’ based system where you can bookmark a page. It’s an application so what is more important is to think about the application states rather than pages.
  • Keep in mind the behavior of ‘Back’ and ‘Forward’ buttons of the browsers. As this is not a ‘Page’ based system. AJAX applications refreshes internal areas with out reloading or changing the ‘Web Page’.
  • Spiders (Search Engine Crawlers) will not be able to index your pages for searches.
  • Users may disable JavaScript in their browsers.

AJAX Quick Start Development Kit

The packages identified in this section are for development purposes only. You need to take extra care to secure the PHP, MySQL and other packages with proper passwords and other security controls. XAMPP is a collection of popular open source tools. Developers at Apache Friends did a great job in bringing all these together and the installation and configuration is absolutely smooth. Download
XAMPP (v1.5.2) from http://www.apachefriends.org/en/index.html available for both Linux and Windows. This will install the following packages.

o Apache 2.2
o PHP (5.1.2)
o MySQL (5.0.20)
o Perl
o FileZilla FTP Server
o Mercury Mail Server

There are lot of popular JavaScript libraries which gives table grids, pull down menus, drag and drop features, tree widgets etc. One of the most famous is ‘script.aculo.us’. Download the latest JavaScript library from
http://script.aculo.us/ and the 'OpenLaszlo' open source development platfom for the Rich Internet Appliations.

Other JavaScript Library Collections
o
http://www.mochikit.com/demos.html
o
http://edevil.wordpress.com/2005/11/14/javascript-libraries-roundup/
o
http://cross-browser.com/
o
http://www.rawdata.net/developer/web_developer/ajax_javascript_librabries.php
o
http://www.ajaxmatters.com/r/resources?id=17

Conclusion

Rich Internet Applications using AJAX technologies looks very promising. Thanks to Google and others they showed how to use existing technologies to change the way we access internet based applications. Most of the software giants like Microsoft, Adobe and others are coming out with AJAX based software development toolkits which shows their commitment to the new model of web based applications. Following sections gives references (links) to AJAX technologies and other resources.

Examples: Popular AJAX Applications on the Web

Google Suggest -
http://www.google.com/webhp?complete=1&hl=en
Google Maps –
http://maps.google.com/
Flickr -
http://www.flickr.com/
Odeo -
http://odeo.com/
Backpack -
http://backpackit.com/
Basecamp -
http://basecamphq.com/

RIA - Software Development Kit

Microsoft Atlas: http://atlas.asp.net/
Adobe Macromedia Flex 2.0: http://www.adobe.com/products/flex/
Backbase Ajax Development Framework: http://www.backbase.com/
Tibco General Interface: http://www.tibco.com/software/ria/gi_resource_center.jsp
JackBeNQ Suite : htttp://www.jackbe.com/
Open Source - OpenLaszlo: http://www.openlaszlo.org/

REST – Representational State Transfer
SOAP – Simple Object Access Protocol
AJAX – Asynchronous JavaScript And XML
ECMA – ECMA-262 Specifications
CSS Cascading Style Sheets

Demo


Sunday, April 02, 2006

Multi Core Mania

In December 2005 I blogged about the new set of programming languages (Metaphor and Fortress - New programming Languages) and how multi core systems could change the computing scenarios. Here is something more on multi core systems. Last week Azul systems announced a 48 way multi core chip, redefining the enterprise computing. However, the current hurdles for the Intel, Sun, IBM and other Hardware vendors with multi core CPUs will be picking the right memory technologies. "If you can't keep the cores fed fast enough from memory, you haven't gained anything," says AMD chief technology officer Phil Hester.
The Vega 1 chip (from Azul) can support a maximum of 384 cores while the new chip supports a whopping 768 cores which takes up 11U of rack space. Like Vega 1, Vega 2 is a 64-bit and all its cores are cache-coherent (fully independent). Intel uses Smart Cache technology to share the level-2 (L2) cache among the cores instead of dedicating a L2 Cache per core. The immediate benefit of this is that both the core has the entire 2MB of L2 cache. The data (in the L2 Cache) can be changed by one Execution Core and subsequently be utilized by the other Execution Core without the need to first write the data to DRAM. As the data is shared with L2 Cache, the traffic on the FSB (Front Side Bus) is minimized.

Today we have single CPU with 8 cores and 32 parallel threads from Sun Microsystems and 48 Core Chip from Azul Systems - imagine the state of these technologies after a decade. GRID computing for the common man (a digitally connected world) will be a reality.

Imagine the year 2015. Parallel computing with Autonomous Executable Entities will be a reality!

Further Reading

  1. Azul Systems – Vega 24 Core chip Azul Compute Appliance
  2. Techworld – Azul launches 48 core processor
  3. Sun Microsystems – Sun UltraSPARC T1 Processor
  4. Sun Microsystems – Multi Core Processor Comparisons
  5. Intel – Dual Core Processor Demo
  6. Intel – Smart Cache: Sharing L2 cache among the cores
  7. Intel – Next leap in multiprocessor Architecture: Intel Dual Core
  8. Intel – Intel Core Duo Processors
  9. Intel – Dual Core Intel Xeon Processors
  10. Silicon Graphics - ccNUMA
  11. Wikipedia – Non Uniform Memory Access (NUMA)
  12. MIT Technology Review - Faster Plastic Circuits
  13. MIT Technology Review – Multicore Mania
  14. MIT Technology Review – Making Multicore Fly
  15. MIT Technology Review – Good bye, Gigahertz
  16. InfoWorld - Sun charges Sun Fire T2000 with UltraSparc
  17. Wikipedia.Com – Cache Coherency
  18. MIT – Cache Coherence
  19. Webopedia.Com – What is Cache Coherence?
  20. PrincetonA survey of Cache Coherence Schemes for Multiprocessors.