Monday, January 30, 2012

Book Review: Programming Concurrency on the JVM

Overview
Programming concurrency is a tough task to get right, just ask Venkat, he'll tell you so!  I attended a presentation by Dr. Venkat Subramaniam on this very subject, Concurrency on the JVM, at my local Java user group a couple months ago and he really caught my attention.  The presentation was a condensed version of the book, and needless to say, I bought the book to help fill in all the details.

Not only is Venkat a 'subject matter expert',  but his presentation kept everyone engaged and laughing!

Contents:
The book is broken down in 5 parts, with 10 chapters total, at just over 250 pages.  The main sections of the book are:

  • Strategies for Concurrency - this discusses the impact of multi-core machines and the three design approaches that are discussed throughout the rest of the book, Shared Mutability, Isolated Mutability and Pure Immutability.

  • Modern Java/JDK Concurrency - covers the Java 1.5+ concurrency features and talks about how to use the newer concurrency classes, like ExecutorService, CountDownLatch, Locks and Fork/Join to solve concurrency problems.

  • Software Transactional Memory - covers Software Transactional Memory (STM), popularized by Clojure, which places access to memory within transactions.  STM is one option for getting away from the 'Synchronize and Suffer' model as Venkat call it.  The intention is to make threaded code more deterministic.   Using the JDK concurrency tools, there is no way to be sure you code is correct because it doesn't always fail when/where you need it to.   By putting the memory access inside transactions, the transaction manager helps resolve the conflicts without explicit locking.

  • Actor-based Concurrency - covers actor design approaches as another option for concurrency design without using synchronization.  This section exercises actors from the Akka library from Scala, Actors from the Groovy library GPars, and mixing Actors and STM as a possible solution.

  • Epilogue - is a short recap of the topics presented previously and points out the scenarios when each of the solutions would be appropriate.
The book lays out a couple example problems and then develops solutions for these problems across a range of JVM languages (Java, Groovy, Scala, Clojure and JRuby).   The examples are short and concise,   enough to help you to get started prototyping against your own concurrency issues.

Summary
This is a good book to get you started on some of these newer concurrency concepts and libraries.  It was an easy read, and I completed the reading fairly quickly, as opposed to the "Java Concurrency in Practice" book, which I started and stopped twice and still have not completed yet!

I would recommend this book to anyone looking to improve their programming skills in the concurrency area.  The book provides you with some alternatives to the old standby of ' Synchronize and Suffer' model.
My last recommendation, if you get the opportunity, attend one of Venkat's presentations, you won't be sorry!

Thursday, January 19, 2012

Passing parameters into Groovy script using Binding class

I recently saw a question posted on the Groovy/Grails group on LinkedIn asking about ways to pass in parameters to a Groovy script.  There were several responses pointing to the CliBuilder class which is certainly one way to handle the problem.   I had just finished reading an article by Ken Kousen in the November issue of GroovyMag  where Ken mentioned another option: using the Binding class.

The example below shows a couple ways of setting variables in the binding, getting the variable values from the binding and how to capture standard output from the script.  The one tricky part is the question "When is something in the Binding and when not?"  The answer is: when it's not defined, it is in the binding!  In the example below, variable c is placed in the binding, but since it is def'd in the script, the value for that variable comes from the local variable rather than the binding.
// setup binding
def binding = new Binding()
binding.a = 1
binding.setVariable('b', 2)
binding.c = 3
println binding.variables

// setup to capture standard out
def content = new StringWriter()
binding.out = new PrintWriter(content)

// evaluate the script
def ret = new GroovyShell(binding).evaluate('''
def c = 9
println 'a='+a
println 'b='+b
println 'c='+c 
retVal = a+b+c
a=3
b=2
c=1
''')

// validate the values
assert binding.a == 3
assert binding.getVariable('b') == 2
assert binding.c == 3 // binding does NOT apply to def'd variable
assert binding.retVal == 12 // local def of c applied NOT the binding!

println 'retVal='+binding.retVal
println binding.variables
println content.toString()
Output
[a:1, b:2, c:3]
retVal=12
[a:3, b:2, c:3, out:java.io.PrintWriter@1e0799a, retVal:12]
a=1
b=2
c=9

Hope this helps!

Tuesday, January 17, 2012

Book Review: SQL Antipatterns Avoiding the Pitfalls of Database Programming

Overview
This is book for software developers that are either new to database programming (SQL) or those that may not have had any formal database education and may have learned 'on-the-job'.

The book is bit longer, approximately 300 pages, than some of the other titles I've read from The Pragmatic Programmers shelf, but it is a very easy read.  The chapters all follow a similar format: Object, Antipattern, How to Recognize the Antipattern, Legitimate Uses of the Antipattern and Solution.  Each chapter the author leads you through a problem that needs correcting within the scope of the database.   Next the antipattern is laid out for you and the author shows the disadvantages of using the antipattern.   Tips are provided to help identify the antipattern and he also suggests some possible legitimate uses of the antipattern.  Finally, the author describes better solutions to the problem presented; ones without the disadvantages discussed previously and solutions that are considered 'best practices' and that will provide better database performance.


Contents
The book is broken down into 4 main sections:

  • Logical Database Design Antipatterns - planning database tables, columns and relationships
  • Physical Database Design Antipatterns - defining tables, indexes and choosing  datatypes
  • Query Antipatterns - SQL command usage, for example, SELECT, UPDATE and DELETE
  • Application Development Antipatterns - correct usage within the scope of a language
Some of that antipatterns described may make you laugh and wonder, "Who the heck would do something like that?" while others you may have run into previously and some might even hit very close to home from your work or personal projects.

There is also an appendix that covers the Rules of Normalization.  I had heard of terms like Normalization, First Normal Form, and Second Normal Form before, but never actually read any explanations. The appendix includes discussions on normalization and first normal form through fifth normal form with examples to help clarify the topics. 

Summary
This is a very good book for software developers just starting to 'cut their teeth' with databases and SQL.  It also serves as a good refresher for those with a bit more database experience.  In my case, I fall into the later scenario.  You can view it as a book of short stories, showing what not to do in these cases.