Wednesday, January 25, 2012

Synchronization Cheat Sheet

Reference : http://www.janeg.ca/scjp/threads/synchronization.html

  • every instance of class Object and its subclass's has a lock
  • primitive data type fields (Scalar fields) can only be locked via their enclosing class
  • fields cannot be marked as synchronized however they can be declared volatile which orders the way they can be used or you can write synchronized accessor methods
  • array objects can be synchronized BUT their elements cannot, nor can their elements be declared volatile
  • Class instances are Objects and can be synchronized via static synchronized methods

Synchronized blocks

  • allow you to execute synchronized code that locks an object without requiring you to invoke a synchronized method
    synchronized( expr ) {
        // 'expr' must evaluate to an Object
    }

Synchronized methods

  • declaring a method as synchronized ie synchronized void f() is equivalent to
    void f() { synchronized(this) {
        // body of method
      } 
    }
  • the synchronized keyword is NOT considered part of a method's signature. IT IS NOT AUTOMATICALLY INHERITED when subclasses override superclass methods
  • methods in Interfaces CANNOT be declared synchronized
  • constructors CANNOT be declared synchronized however they can contain synchronized blocks
  • synchronized methods in subclasses use the same locks as their superclasses
  • synchronization of an Inner Class is independent on it's outer class
  • a non-static inner class method can lock it's containing class by using a synchronized block
    synchronized(OuterClass.this) {
        // body 
    }

Locking

  • locking follows a built-in acquire-release protocol controlled by the synchronized keyword
  • a lock is acquired on entry to a synchronized method or block and released on exit, even if the exit is the result of an exception
  • you cannot forget to release a lock
  • locks operate on a per thread basis, not on a per-invocation basis
  • Java uses re-entrant locks ie a thread cannot lock on itself
class Reentrant {

  public synchronized void a() {
      b();
      System.out.println("here I am, in a()");
  }
  public synchronized void b() {
      System.out.println("here I am, in b()");
  }
}
  • in the above code, the synchronized method a(), when executed, obtains a lock on it's own object. It then calls synchronized method b() which also needs to acquire a lock on it's own object
  • if Java did not allow a thread to reacquire it's own lock method b() would be unable to proceed until method a() completed and released the lock; and method a() would be unable to complete until method b() completed. Result: deadlock
  • as Java does allow reentrant locks, the code compiles and runs without a problem
  • the locking protocol is only followed for synchronized methods, it DOES NOT prevent unsynchronized methods from accessing the object
  • once a thread releases a lock, another thread may acquire it BUT there is no guarantee as to WHICH thread will acquire the lock next

Class fields and methods

  • locking an object does not automatically protect access to static fields
  • protecting static fields requires a synchronized static block or method
  • static synchronized statements obtain a lock on the Class vs an instance of the class
  • a synchronized instance method can obtain a lock on the class
    synchronized(ClassName.class) {
        // body 
    }
  • the static lock on a class is not related to any other class including it's superclasses
  • a lock on a static method has no effect on any instances of that class (JPL pg 185)
  • you cannot effectively protect static fields in a superclass by adding a new static synchronized method in a subclass; an explicit block synchronization is the preferred way
  • nor should you use synchronized(getClass()); this locks the actual Class which might be different from the class in which the static fields are declared

Bitwise operations and samples

Notes-1 : http://leepoint.net/notes-java/data/expressions/bitops.html

Friday, January 6, 2012

Thursday, December 29, 2011

All about EJBs

EJB-1.0 : Basic Components - Overview (Remote Interface & Remote Home version):
Java World Link

Build your first stateless session bean:
Sun Link

EJB 2.0 : Getting Started with Enterprise JavaBeans (Uses Remote Interface and Remote Home Interface version even though local are available):
ConceptGo Link

Restrictions:
"This section describes the programming restrictions that a Bean Provider must follow to ensure that the enterprise bean is portable and can be deployed in any compliant EJB 2.0 Container. The restrictions apply to the implementation of the business methods...

* An enterprise Bean must not use read/write static fields. Using read-only static fields is allowed. Therefore, it is recommended that all static fields in the enterprise bean class be declared as final.
* An enterprise Bean must not use thread synchronization primitives to synchronize execution of multiple instances.
* An enterprise Bean must not use the AWT functionality to attempt to output information to a display, or to input information from a keyboard.
* An enterprise bean must not use the java.io package to attempt to access files and directories in the file system.
* An enterprise bean must not attempt to listen on a socket, accept connections on a socket, or use a socket for multicast.
* The enterprise bean must not attempt to query a class to obtain information about the declared members that are not otherwise accessible to the enterprise bean because of the security rules of the Java language. The enterprise bean must not attempt to use the Reflection API to access information that the security rules of the Java programming language make unavailable.
* The enterprise bean must not attempt to create a class loader; obtain the current class loader; set the context class loader; set security manager; create a new security manager; stop the JVM; or change the input, output, and error streams.
* The enterprise bean must not attempt to set the socket factory used by ServerSocket, Socket, or the stream handler factory used by URL.
* The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread; or to change a thread's priority or name. The enterprise bean must not attempt to manage thread groups.
* The enterprise bean must not attempt to directly read or write a file descriptor.
* The enterprise bean must not attempt to obtain the security policy information for a particular code source.
* The enterprise bean must not attempt to load a native library.
* The enterprise bean must not attempt to gain access to packages and classes that the usual rules of the Java programming language make unavailable to the enterprise bean.
* The enterprise bean must not attempt to define a class in a package.
* The enterprise bean must not attempt to access or modify the security configuration objects (Policy, Security, Provider, Signer, and Identity).
* The enterprise bean must not attempt to use the subclass and object substitution features of the Java Serialization Protocol.
* The enterprise bean must not attempt to pass this as an argument or method result. The enterprise bean must pass the result of SessionContext.getEJBObject(), SessionContext. getEJBLocalObject(), EntityContext.getEJBObject(), or EntityContext.getEJBLocalObject() instead."

Interview Questions:
AppLabs Link

Core J2EE Patterns


Reference : Sun Developer Network

Tuesday, December 27, 2011

Java - DBMS

Java - Stored Procedure tutorial (From creation of class - loading, publishing, executing, trigger) :

Java Docs

Sunday, December 25, 2011

All about volatile

References:
JeremyManson Blog

Javamex

When to use volatile : http://www.javamex.com/tutorials/synchronization_volatile_when.shtml

Example usecase of volatile : Refer to javamex

Infamous Double-checked locking for singletons. - TBD

Saturday, December 24, 2011

Generics

Must read tutorial:
Sun Java Tutorial

Comparator and Comparable

References:
Link1:
http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html

Link2:
http://www.java2s.com/Code/Java/Collections-Data-Structure/WritingYourownComparator.htm

Friday, December 23, 2011

Hashcode & equals

Contract between hashcode and equals as per Java specification -

* Whenever it is invoked on the same object more than once during an execution of a Java application, the hashcode() method must consistently return the same integer, provided no information used in equals() comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

* If two objects are equal according to the equals(object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.

* It is NOT required that if two objects are unequal according to the equals(Java.lang.Object) method, then calling the hashCode() method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

--------------------------------------------
Sample Buggy code to show bug if same fields are not used to compute equals and hashCode :
--------------------------------------------
package com.examples;

public final class Box {

private final int ssn;
private final int age;

public Box(int ssnval, int ageval) {
ssn = ssnval;
age = ageval;
}

// Magic number 31 (odd and prime) as seed just like java String.
public int hashCode() {
return 31 + ssn;
}

public boolean equals(Object that) {

if (this == that) {
return true;
}

if (! (that instanceof Box) ) {
return false;
}

if ((this.ssn == ((Box)that).ssn) && (this.age == ((Box)that).age)) {
return true;
}

// Bug in code as there is a mismatch of fields between equals and hashcode.
return false;
}

}

package com.examples;

import java.util.*;

public class Test {

public static void main(String[] args) {
Box b1 = new Box(10, 20);
Box b2 = new Box(10, 25);

if (b1.hashCode() == b2.hashCode()) {
System.out.println("Box objects b1 & b2 - hashcodes are same");
} else {
System.out.println("Box objects b1 & b2 - hashcodes are NOT same");
}

if (b1.equals(b2)) {
System.out.println("Box objects b1 & b2 - equals are same");
} else {
System.out.println("Box objects b1 & b2 - equals are NOT same");
}

Map boxmap = new HashMap();
boxmap.put(b1, "TestVal");

String val1 = boxmap.get(b1);
String val2 = boxmap.get(b2);
System.out.println("Val1 = " + val1);
System.out.println("Val2 = " + val2);

}

}

Output demonstrating the bug : val2 is null because of broken contract.
-------------------------------
Box objects b1 & b2 - hashcodes are same
Box objects b1 & b2 - equals are NOT same
Val1 = TestVal
Val2 = null


The above example clearly shows that Java collection uses hashCode first to go to the appropriate bucket. Then uses equals() to see if object matches.

Bug fix: Change hashCode() to use 31 + ssn + age.

*** Note that the above sample uses Immutable Box object demonstration - all final, private and one-shot object construction.


References:
------------
* Implementing HashCode - http://www.javapractices.com/topic/TopicAction.do?Id=28

* http://tech-read.com/2009/02/12/use-of-hashcode-and-equals