PC - Open Ports

Today I found the following command very very useful. I have a DB2 database running locally and I have no problem connecting to it using DB2 Control Center. But when I use Aquadata studio.. ADS requires the port number to connect to... which I dont know and DB2 Control Center does not tell.

So I needed a command to see what all ports my PC had opened. And the following command came to my rescue.

netstat -an | find /i "listening"

Replace dot with comma

We have a requirement in our web application to replace the dot pressed in the Numeric pad, with comma. I 've searched in the internet for a solution for this and could not find one.
Hence I had to find the following solution. (Works only in IE browser)

var dotReplace = false;
function keypressEvent(the_key) {
if(the_key == 46 && dotReplace) {
event.keyCode = 44;
}
}
function keydownEvent(the_key) {
if(the_key == 110) {
dotReplace = true;
} else {
dotReplace = false;
}
}


And in the form call the above functions for respective events.
<input name="disp" type="text" 
onkeypress="keypressEvent(event.keyCode)"
onkeydown="keydownEvent(event.keyCode)">

Working Example:
Type a dot from the numeric pad here:

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