LendKey

Tuesday, February 10, 2009

WIFI slowness of my Macbook Pro

My Macbook Pro (2007 edition)'s WIFI turned to be very slow recently. Speed test (http://www.speedtest.net) sometime report only 10K download while I have 15M Comcast broadband.
I struggled many days and tried different ways I could imagine, even bough a new router, problem still unresolved.

Today, I tried to check /var/log/system.log and found lots of error messages like:

Feb 9 01:10:06 li-mac-2 mDNSResponder[22]: AppendDNSNameString: Illegal empty label in name "."
Feb 9 01:10:06 li-mac-2 mDNSResponder[22]: RegisterSplitDNS: bad domain .


Then I Googled the keywords:mDNSResponder[22]: RegisterSplitDNS: bad domain . and found a hint at:
http://discussions.apple.com/message.jspa?messageID=6911574


Followed suggestion from the site, I finally managed to resolve my issue. Since the DNS error msg is gone, my MacBook Pro could do 14Mbps on wifi.

Here's summary of what I did:
1. run scutil --dns and saw something like:

DNS configuration

resolver #1
nameserver[0] : 192.168.0.1
order : 200000

resolver #2
domain : .
options : pdns
timeout : 5
order : 150000

resolver #3
domain : local
options : mdns
timeout : 2
order : 300000
...


2. Open Keychain Access
3. Click on System at left side
4. Find and delete item named DNS Key
5. Reboot
6. Problem resolved!

Friday, January 23, 2009

Thread-safe singlton instantiation

Very often that we need to get instance of a single which is initialized Lazily, meaning only when it is first be needed.

What we normally do is:



After reading a scary article about Java memory model, I realized this code does not always work in a multi-threaded environment:
http://www.ibm.com/developerworks/library/j-jtp03304/
and
http://www.ibm.com/developerworks/java/library/j-jtp02244.html
and
http://www.javaworld.com/jw-02-2001/jw-0209-double.html

The idea is complicated, but to describe it in a simple sentence, I would say:
Because modern compiler might reorder execution sequence of the commands, the code might fail under certain hardware/software environment.

So a safer solution is:


Because JVM guarantee initialization and execution order static inner classes, this solution is not only safe, but also faster than using volatile or synchronized keywords.

Thursday, January 22, 2009

Javascript compressors

I ran into a nice websites that compares JS compressors on the fly. Cool stuff. So I write down here as a reference for myself, and you, just in case:
http://compressorrater.thruhere.net/

Wednesday, January 14, 2009

Style check for Javascript files

Like PMD/StyleCheck for Java, I finally found a tool to perform coding style check and also to find common mistakes from JavaScript files:
http://www.javascriptlint.com

Check their sample and doc for details. It's not so comprehensive/powerful as PMD/StyleCheck, but should be a good start for JS.

Sunday, January 11, 2009

Change default Java version on Mac

The default Java on Mac (as of today) is 1.5.
I went to Apple's site and downloaded 1.5 update which includes 1.6 64bit. But the download page specifically said it does not change Mac's default setting.
In order to use the new Java 1.6 as the default JVM, need to do the following after the update is downloaded and installed:

1. Run /Aplication/Utilities/Java/Java Preferences
This is screen on my MacBookPro:

2. Click and drag Jave SE 6 64-bit to top of both list, and exit the program.

Now open a terminal, run
java -version

The result should be:

java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06-153)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_07-b06-57, mixed mode)

Thursday, January 1, 2009

RAR command line - to exclude files and folders

After many try&fail, I finally figured how to exclude files and subfolders when archiving with RAR command line.
Basically I tried to archive a folder with .svn subfolders. I definitely want to exclude them from the archive. But when I tried:
rar a -r -x.svn myarchive 

It did not exclude any .svn folders in second or deeper level of folder hierarchy.
Now the correct way is:
rar a -r  -x.svn -x*/.svn -x*/.svn/* -x*/anotherSubFolder -x*/anotherSubFolder/* myarchive

It will exclude all folder and subfolders with name .svn or anotherSubFolder.
Happy Year 2009!

Li

Wednesday, December 24, 2008

Why do we do final in Java?

Noticed lots of nice opensource code and Java style checker suggested to use final and static as much as possible.
I kinda understand why to use static, especially for methods, but not sure why final will help.
After a bit of google, I found the following claim at:

http://www.javaperformancetuning.com/tips/final.shtml#REF1

From: http://www.protomatter.com/nate/java-optimization/
Various tips. (Page last updated 1999?, Added 2000-10-23, Author Nate Sammons, Publisher Sammons). Tips:

Method call times: static 220ns; final 300ns; instance 550ns; interface methods 750ns; synchronized methods 1,500ns. [But times vary enormously depending on the VM and context].
Use static final methods where possible. [And do functional programming too ;-)]


Since the note was added around 1999 and 2000, I'm wondering it still stand true or not.

Now matter what, I will try my best to apply this policy to my code. But if anyone can explain why to me, it will make me feel much better.

Thanks!