Java Synchronize


when to use synchronize?
synchronized should be used whenever shared data is being modifed. Shared data can be variables declared as static or data stored in request/session

what happens in unsynchronized code?
In the above situation if the code is not synchronized there is a possibility that 2 threads might try to modify the data simultaneously and the data might get corrupted.

what does synchronize do?
At any point of time only one thread can execute the Synchronized block of code. So no possibility of data corruption.

how to synchronize?
Synchronization always requires a Lock. Compiler allows the thread that has the Lock to enter the synchronized code. And it is only possible for one thread to have the Lock at a time. If 2 threads tries to get the Lock then only one succeeds and the second one will have to wait till the first one releases the Lock.

what is a Lock?
every object in java is created with one lock.

Code can be synchronized at method level or for few lines of code.
If code is synchronized at method level then by default the Threads will get a Lock on the class object of method.
Example:
public synchronized void modifyCommonData() {
...
}

If the code is synchronized at block of code level then the Lock of the object passed to synchronized will be used
Example:
String myOwnLock = "myOwnLock";
synchronized(myOwnLock) {
....
}

CAUTION:
Synchronized restricts simultaneous execution of the code by multiple threads. Basically the Threads wait in a Queue to get their chance to execute the synchronized code.
- So if a method does a lot of things then it is better to use the synchronized blocks than synchronizing the whole method.
- And it is not necessary to use synchronized for getMethods (if they are just reading the data)

Useless data types: Date, Date, Time, Timestamp

The Seven Habits of Highly Dysfunctional Design

Instead, someone at IBM came up with an idea of how to sabotage Java's simplicity: Donate the Taligent code to it! In a plan insidious enough to have been hatched by Larry Elison himself (if he weren't too busy going through Microsoft dumsters at the time), IBM got Sun to agree to put Taligent code into Java, thus ensuring that IBM Global Services would have ample work for the next ten years. It also ensured that the rational (no pun intended) Java developers would forever be taunted by the following license details in all Taligent-cursed code:

 * The original version of this source code and documentation is copyrighted
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
* materials are provided under terms of a License Agreement between Taligent
* and Sun. This technology is protected by multiple US and International
* patents. This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.

.... For more details... http://jroller.com/page/cpurdy?entry=the_seven_habits_of_highly

JSP: Avoid creating a session

To avoid creating session when a JSP is accessed add the following page directive in the JSP.

<%@ page session="false" %>

MDC NullPointerException in WSAD debug


I was using the Log4J 's MDC class to store some values in the Thread for logging.
When I started my WAS Test Environment server in debug mode...
I received a NullPointerException in java.lang.InheritableThreadLocal.java (69)
The Source code for this class says...
66| public Object get() {
67|     Thread ourThread = Thread.currentThread();
68|     Map map = ourThread.inheritableThreadLocals;
69|     Object value = map.get(key);

After searching on the internet found that this is actually a bug in the JRE of WSAD.
The way around for this problem is to disable the "hot method replace" functionality in WSAD.

Link: Apache Mail archive link

CSS: The Road to Enlightenment

This is a very good site which explains how powerful CSS is.

CSS: Table row mouseover styles

This a lesson I learnt today.
My requirement: I have a table of data and I wanted to display a different background color when I move the mouse on any part of the row. So I defined seperate styles for the TR and TD tags. For the TR tag I also added onmouseover code to change the styleclass to a different one.
tr.datarow {background-color:white; color:black;}
tr.datarow-onmouseover {background-color:blue; color:white; cursor:hand; }
<tr onmouseover="this.className='datarow-onmouseover' " onmouseout= "this.className= 'datarow'" class="datarow"> 

Problem: Though the onmouseover is triggered when I move the mouse on the rows, it doesnt change the background color.
Solution: To change a property of TD on mouse over of TR then the property should not be overwritten in the TD styles. i.e. I 've defined a style for the TD tag also. And this style also contains background-color and color and properties. These are taking precedence over the TR properties.

In general, I think, in CSS if the parent and its child has same style property then overwriting the parents property does NOT overwrite the childs properties.

How to setup Icon for a web application

The following code in index.jsp displayed the icon when the page is accessed from FireFox.

<link rel="icon" href="<%=request.getContextPath()%>/img/favicon.ico" type="image/ico">
<link rel="shortcut icon" href="<%=request.getContextPath()%>/img/favshortcut.ico" type="image/ico">

To get it work in Internet Explorer... just copy the favicon.ico to the root WebContent directory.