Showing posts with label Java 7. Show all posts
Showing posts with label Java 7. Show all posts

Sunday, September 23, 2012

Is Java Dead or Invincible?


Is Java Dead?

Writer Isaac Asimov once said that "the only constant is change".  That isn't just a phrase in the software industry, it is an absolute fact.  Once, there was a day when Corba was king but it was usurped by Web Services.  Even within the world of Web Services, that used to be all about SOAP but now it's REST style services which are much more popular today.  Now somethings will obviously hang around a bit longer than others.  Relational databases have been around 40 years and aren't going to be kicked out by NoSql just yet. The HTTP protocol has been at version 1.1 since 1999 and has helped us use a thing called the Internet.  As for Java well that has been a pretty popular computer programming language for the last decade and a half.

According to Dutch research firm Tiobe in terms of overall popularity, Java ranked 5th in 1997, 1st in 2007 and 2nd in Sept 2012. At the time of writing there are over 2,000 Java progamming books on Amazon in English and there are almost 300,000 threads on Stackoverflow related to Java.   However, as George Orwell once said: "Whoever is winning at the moment will always seem to be invincible". But is Java invincible or beginning to die?  That's the question being asked more and more now.

In my humble opinion, the challenges to Java can be split into three categories:
  1. The rise of alternative languages
  2. Scalability / Multi-Core processors
  3. The return of the fat client.
Let's elaborate...

The rise of alternative languages

Alternative languages can be split into two groups: those that run on the JVM (Scala, Groovy etc)
and those that don't (Python, Ruby).  One interesting thing is that the first group is pretty large.  The languages that run on the JVM aren't mutual exclusive to Java and in a sense strengthen it reminding us what a remarkable piese of software engineering the JVM is.  Development teams can get that
extra bit of expressiveness in a niche language like Groovy, but can still call out to Java when they need some cool Java library or just need that extra bit of performance.  Remember the advantages in Groovy 2.0 speed it up but it is still not as fast as Java.

As for the features some of these languages provide that are not in Java, well that is the case but it won't always be the case.  Take a look at the roadmap for Java 8 and the features it will include. Just like Java EE 5 and 6 took ideas from Spring / Seam,  the Java lanuage in its 8th major release will be taking ideas from other languages. For example literal functions will be facilitated by Lambdas.  Java 8 Lamdas will have support for type inference and because they are just literals it will be possible to pass them around (and return them) just like a String literal or any anonymous Object.

That means instead of having to write an implementation of Comparator to pass to the Collections sort utility to sort a list of Strings, in Java 8 we will just do:
Collections.sort(list, (s1, s2) -> s1.length() - s2.length());

So, the alternative JVM languages don't exactly kick Java out of the party. It is still there, but in a party that has a better selection of music played and in a party where the guests encourages their host to be a better host.

Scaling on the multi-core platforms

As for multi-core and JVM - we all know that with a JVM running on a single core it was possible to spawn threads in the very first release of Java.  But these threads weren't executing in parallel, the CPU switched between them very quickly to create the impression that they were running in parallel. JStack may tell you that 50 threads have state "runnable" on your single core machine but this just means they are either running or eligible to run.  With multi-core CPUs it is possible to get true parallelism.  The JVM decides when to execute threads in parallel.

So what's the deal here?  Firstly, even though concurrency and threads were a feature of Java from the very beginning the language support was limited meaning development teams were writing a lot of their own thread management code - which could get ugly very quickly. This was alleviated greatly in JDK 1.5 with the arrival of a range of thread management features in the java.util.concurrent package.  Secondly, to get better parallelism something else was needed.  This came in Java 7 with Doug Lea's Fork / Join framework which uses clever techniques such as work stealing and double sided queues to increase parallelism. However, even with this Framework decomposing (and re-arranging) data is still a task that is needed to be done by the programmer.

Functional progamming gives us another option to perform computations on data sets in parallel.  
In Scala, for example, you just pass the function you wish to operate on the data and tell scala you want the computation to be parallelised.
outputAnswer((1 to 5).par.foreach(i => longComputation))

And guess what? The same will be available in Java 8.
Array.asList(1,2,3,4,5).parallel().foreach(int i ->heavyComputation())
Since scalability and performance are architectural cousins, it is worth stating that in many experiements Java still out performns other languages.  The excellent Computer Language Benchmark Game shows Java outperformaning many languages. It hammers the likes Perl, PHP, Python3, Erlang in many tests, beats Clojure, C# in nearly all tests and is only just behind C++ in terms in the performance results. Now,  performance tests can't cover everything and a context will always have some bias which favours one language over another but going by these tests it is not as if Java is a slow coach.

The fat client is back!

The return of the fat client

Since the advent of AJAX, Doug Crockford telling people how to use JavaScript and the rise of an excellent selection of JavaScript libraries the fat client is truely back.  Close your eyes and imagine what a cool single page web application such as gmail would look and feel like if it was just thin client web framework based on Spring MVC, JSF or Struts - you just cannot beat the performance of a well designed fat client.

One saving grace is that JavaScript is a lot more difficult to be good than some people think. It takes a lot more thinking to really understand  Closures, Modules and the various JavaScript best practises than it does to know your way around a web framework like Spring MVC and Struts. In addition, building a single page web application (again such as gmail) doesn't just require excellent JavaScript understanding it requires understanding of how the web works. For example, browsers don't put Ajax requests in the browser history.  So you gotta do something smart with fragment identifiers if you want the back and forward buttons to be usable and meaningful for the User.

There is a probably some room here for a hybrid approach which uses both a web framework and JavaScript and of course some JavaScript libraries.   This gives developers a structure to build an application and then the opportunity to use JavaScript, JQuery or whatever cool library takes there fancy for important parts of the Application that need to be jazzed up.   In a true fat web client approach, there should be no HTML served from the server (that means no JSPs), the only thing coming back from the server is data (in the form of JSON). However,  using a hybrid approach you can make a transition from thin to fat easier  and you can still put your JavaScript libraries on a CDN, you just won't get all the advantages of a full fat web client approach.

Summary

In summary, Java has had some bad moments. AWT was a rush job, Swing had performance problems, the early iterations of  EJB were cumbersome and JSF was problematic in comparison to other frameworks such as Struts and Spring MVC.  But, even today, extremely innovative projects such as Hadoop are built using Java.  It still has massive support from the open source community. This support has not only helped Java but also show Java some its problems and things it needs to get better at.   Java has shown it has the ability to evolve further and while other languages challenge it I don't think the game is over just yet.  It goes without saying, a lot will of the future of Java will depend on Oracle but let's hope whatever happens that the winner will be technology.

References

  1. Yammer and their migration to scala
  2. James Gosling taking about the state and future of Java at Google tech talk
  3. Article from Oracle describingFork and Join in Java 7
  4. Eric Bruno:Building Java Multi-Core applications
  5. Edgardo Hernandez:Parallel processing in Java
  6. IEEE top ten programming languages
  7. JDK 8 downloads
  8. Java Code Geeks article on Fork Join
  9. Good Fork Join article by Edward Harned
  10. Fork / Join paper from Doug Lea
  11. Fork / Join Java updates information from Doug Lea
  12. Scala Java Myths - great article by Urs Peter and Sander van der Berg

Tuesday, December 27, 2011

JAXB, SAX, DOM Performance

This post investigates the performance of unmarshalling an XML document to Java objects using a number of different approaches. The XML document is very simple. It contains a collection of Person entities. 

There is a corresponding Person Java object for the Person entity in the XML
...

and a PersonList object to represent a collection of Persons. 

The approaches investigated were:
  • Various flavours of JAXB
  • SAX
  • DOM
In all cases, the objective was to get the entities in the XML document to the corresponding Java objects. The JAXB annotations on the Person and PersonList POJOS are used in the JAXB tests. The same classes can be used in SAX and DOM tests (the annotations will just be ignored). Initially the reference
implementations for JAXB, SAX and DOM were used. The Woodstox STAX parsing was then used. This would have been called in some of the JAXB unmarshalling tests.

The tests were carried out on my Dell Laptop, a Pentium Dual-Core CPU, 2.1 GHz running Windows 7.

Test 1 - Using JAXB to unmarshall a Java File.


Test 1 illustrates how simple the progamming model for JAXB is. It is very easy to go from an XML file to Java objects. There is no need to get involved with the nitty gritty details of marshalling and parsing.

Test 2 - Using JAXB to unmarshall a Streamsource

Test 2 is similar Test 1, except this time a Streamsource object wraps around a File object. The Streamsource object gives a hint to the JAXB implementation to stream the file.


Test 3 - Using JAXB to unmarshall a StAX XMLStreamReader

Again similar to Test 1, except this time an XMLStreamReader instance wraps a FileReader instance which is unmarshalled by JAXB.


Test 4 - Just use DOM
This test uses no JAXB and instead just uses the JAXP DOM approach. This means straight away more code is required than any JAXB approach.
Test 5 - Just use SAX Test 5 uses no JAXB and uses SAX to parse the XML document. The SAX approach involves more code and more complexity than any JAXB approach. The Developer has to get involved with the parsing of the document.

The tests were run 5 times for 3 files which contain a collection of Person entities. The first first file contained 100 Person entities and was 5K in size. The second contained 10,000 entities and was 500K in size and the third contained 250,000 Person entities and was 15 Meg in size. In no cases was any XSD used, or any validations performed. The results are given in result tables where the times for the different runs are comma separated.

TEST RESULTS
The tests were first run using JDK 1.6.26, 32 bit and the reference implementation for SAX, DOM and JAXB shipped with JDK was used.

Unmarshall Type 100 Persons time (ms) 10K Persons time (ms)  250K Persons time (ms)
JAXB (Default)  48,13, 5,4,4 78, 52, 47,50,50 1522, 1457, 1353, 1308,1317
JAXB(Streamsource) 11, 6, 3,3,2 44, 44, 48,45,43 1191, 1364, 1144, 1142, 1136
JAXB (StAX) 18, 2,1,1,1 111, 136, 89,91,92 2693, 3058, 2495, 2472, 2481
DOM 16, 2, 2,2,2 89,50, 55,53,50 1992, 2198, 1845, 1776, 1773
SAX 4, 2, 1,1,1 29, 34, 23,26,26 704, 669, 605, 589,591
JDK 1.6.26 Test comments

  1. The first time unmarshalling happens is usually the longest.
  2. The memory usage for the JAXB and SAX is similar. It is about 2 Meg for the file with 10,000 persons and 36 - 38 Meg file with 250,000.  DOM Memory usage is far higher.  For the 10,000 persons file it is 6 Meg, for the 250,000 person file it is greater than 130 Meg. 
  3. The performance times for pure SAX are better. Particularly, for very large files.
The exact same tests were run again, using the same JDK (1.6.26) but this time the Woodstox implementation of StAX parsing was used.

 
Unmarshall Type 100 Persons time (ms) 10K Persons time (ms)  250K Persons time (ms)
JAXB (Default)  168,3,5,8,3 294, 43, 46, 43, 42 2055, 1354, 1328, 1319, 1319
JAXB(Streamsource) 11, 3,3,3,4 43,42,47,44,42 1147, 1149, 1176, 1173, 1159
JAXB (StAX) 30,0,1,1,0 67,37,40,37,37 1301, 1236, 1223, 1336, 1297
DOM 103,1,1,1,2 136,52,49,49,50 1882, 1883, 1821, 1835, 1822
SAX 4, 2, 2,1,1 31,25,25,38,25 613, 609, 607, 595, 613
JDK 1.6.26 + Woodstox test comments

  1. Again, the first time unmarshalling happens is usually proportionally longer.
  2. Again, memory usage for SAX and JAXB is very similar. Both are far better
    than DOM.  The results are very similar to Test 1.
  3. The JAXB (StAX) approach time has improved considerably. This is due to the
    Woodstox implementation of StAX parsing being used.
  4. The performance times for pure SAX are still the best. Particularly
    for large files.
The the exact same tests were run again, but this time I used JDK 1.7.02 and the Woodstox implementation of StAX parsing.

Unmarshall Type 100 Persons time (ms) 10,000 Persons time (ms)  250,000 Persons time (ms)
JAXB (Default)  165,5, 3, 3,5 611,23, 24, 46, 28 578, 539, 511, 511, 519
JAXB(Streamsource) 13,4, 3, 4, 3 43,24, 21, 26, 22 678, 520, 509, 504, 627
JAXB (StAX) 21,1,0, 0, 0 300,69, 20, 16, 16 637, 487, 422, 435, 458
DOM 22,2,2,2,2 420,25, 24, 23, 24 1304, 807, 867, 747, 1189
SAX 7,2,2,1,1 169,15, 15, 19, 14 366, 364, 363, 360, 358
JDK 7 + Woodstox test comments:

  1.  The performance times for JDK 7 overall are much better.   There are some anomolies - the first time the  100 persons and the 10,000 person file is parsed.
  2. The memory usage is slightly higher.  For SAX and JAXB it is 2 - 4 Meg for the 10,000 persons file and 45 - 49 Meg for the 250,000 persons file.  For DOM it is higher again.  5 - 7.5 Meg for the 10,000 person file and 136 - 143 Meg for the 250,000 persons file.
Note: W.R.T. all tests
  1. No memory analysis was done for the 100 persons file. The memory usage was just too small and so it would have pointless information.
  2. The first time to initialise a JAXB context can take up to 0.5 seconds. This was not included in the test results as it only took this time the very first time. After that the JVM initialises context very quickly (consistly < 5ms). If you notice this behaviour with whatever JAXB implementation you are using, consider initialising at start up.
  3. These tests are a very simple XML file. In reality there would be more object types and more complex XML. However, these tests should still provide a guidance.
Conclusions:
  1. The peformance times for pure SAX are slightly better than JAXB but only for very large files. Unless you are using very large files the performance differences are not worth worrying about. The progamming model advantages of JAXB win out over the complexitiy of the SAX programming model.  Don't forget JAXB also provides random accses like DOM does. SAX does not provide this.
  2. Performance times look a lot better with Woodstox, if JAXB / StAX is being used.
  3. Performance times with 64 bit JDK 7 look a lot better. Memory usuage looks slightly higher.

Tuesday, July 12, 2011

Java SE 7

JDK 1.0 was released on the 23rd January 1996. Then came:
  • JDK 1.1 (Feb 1997)
  • J2SE 1.2 (Dec '98)
  • J2SE 1.3 (May '00)
  • J2SE 1.4 (Feb '02)
  • J2SE 5.0 (sept 04)
  • Java SE 6 (Dec '06)
Each of these major releases has had minor releases which dealt with less important things (such as fixing bugs!) and after the 26th update for Java SE 6, it's now finally time for Java SE 7 to be released - officially.  Codenamed Dolphin Java SE 7 has had several preview releases, the 28th of July is the day the binaries get an official release. Incidently, not too many household names have their birthday day on the 28th July (unless you're familiar with Greek singer Nacy Alexiadi, American actor Tom Pelphrey or English Soccer player Michael Carrick) so perhaps the 28th is now a good reason to light a candle on the day which also saw Austria-Hungary declare war on Serbia (thank you Google, Wikipedia...). Having worked on every major release of Java except 1.0, I have to say I'm excited about Java SE 7.0.  Especially since no language changes (i.e. programming constructs) were introduced in Java SE 6 (even though some cool ones were introduced in Java SE 5 such as generics, autoboxing, varargs...)

Language changes is the area that I find most interesting because it is what developers will get to use in their day to day jobs. It's what people will notice. It's what people will talk about. Now, in Java SE 7 the language development was under a sub-project known as Project Coin.  There was an open call for proposals between Feb 27th, 2009 and March 30, 2009 and in total 70 proposals received some extensive discussion. The 70 were then narrowed down to five or so and now we're going to take a look at the ones that are part of Java SE 7.

Binary Literals In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number. Type - inference for Generics JDK 1.5 saw the introduction of generics. One of the great advantages of generics was cleaner code because the need for casting was eliminated (if the generics feature was used correctly!).  However, unless you enjoyed those annoying compile time warnings about type safety you had to generify both the declaration and instantiation. For example, suppose you had a list of tennis balls... The annoying thing about having to do this is that it re-introduced code bloat - something generics was trying to reduce.  Now in Java SE 7 there is type inference for generic instance creation.  You can substitute the parameterized type of the constructor with an empty set of type parameters <> (also known as the diamond operator).  That means you can do: and you won't get any annoying warning but you'll still get all the benefits of compile time type safety provided by generics.

Catching multiple exceptions I really like this one.  Say you're catching multiple exceptions where some of the exceptions don't form an inheritance hierarchy but share the same exception handling code. You're going to end up with code bloat. For example: With Java SE 7, you can now do: Not bad eh - bound to tidy up tonnes of code. In addition to the ability to catch mulitple exceptions there is also some new precision available regarding re-throwing of exceptions.  

Underscores in literals Underscores can be used in literals now to make them more easier to read:

Strings can be used in Switch statements Switch statements are useful. They can be used with primitive types and enumerated types. In Java SE 7, they can also be used with Strings. Note: this simple but useful feature was officially suggested from the lead engineer of Project Coin, Joseph D. Darcy.  You can check out his blog here.

Autoclosing of resources (also known as try-with-resources) This is a classic programming error. You use a resource (e.g. a JDBC connection) and you forget to close it.  Now, once the resource implements java.io.AutoClosable (or the interface java.io.Closeable) you don't need to close it. Yippee. The list of classes in the JDK that implement this in the JDK is pretty extensive.  Just check the JDoc. for the AutoCloseable interface. Interestingly this proposal came from Joshua Bloch, the acclaimed author of the stupendous Effective Java - a must read for all serious programmers. Note in addition to the new progamming features, Java SE 7 also includes:

  • Increased support for dynamic languages (such as Ruby and Python)
  • Improved security support. Eliptic-curve crytography (ECC) implementation will be included.
  • Improved class loader support
  • and much more

Check it out...

References

  1. Binary literals and underscores in literals http://blogs.oracle.com/darcy/entry/project_coin_literal_grammar_hackery
  2. Strings in switch http://blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy
  3. Diamond operator http://mail.openjdk.java.net/pipermail/coin-dev/2009-November/002393.html and  http://download.java.net/jdk7/docs/technotes/guides/language/type-inference-generic-instance-creation.html
  4. Improved exception handling http://blogs.oracle.com/darcy/entry/project_coin_multi_catch_rethrow
  5. Simplified varargs method invocation http://blogs.oracle.com/darcy/entry/projectcoin_inducing_contributory_pollution
  6. Try-with-resources http://blogs.oracle.com/darcy/entry/project_coin_try_out_try
  7. Java Programming Language Feature enhancements http://download.java.net/jdk7/docs/technotes/guides/language/enhancements.html
  8. Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods http://download.java.net/jdk7/docs/technotes/guides/language/non-reifiable-varargs.html