Thursday, December 9, 2010

Linux tips

> ls -l | grep -v total | awk '{ S+=$5} END { print "Total = ",S, ", Average = ", S/NR, ", Entries = ", NR, ", Columnss = ", NF}'
Total = 70238 , Average = 4389.88 , Entries = 16 , Columnss = 9
================================================================
> x=2008
> y=1957
> echo "$[$x - $y]"
51
> echo "$(($x - $y))"
51

================================================================
> gunzip < /usr/share/man/man8/vgrename.8.gz | nroff -man less

================================================================
> for i in `find /usr/home -name '*.jar'`
> do
> jar tvf $i | grep Something 2>/dev/null
> if [ $? -eq 0 ]; then
> echo $i
> fi
> done

================================================================
> groovy -e "System.env.each { println it }"
> groovy -e "System.props.each { println it }"

How to find the locking order for equals() ?

Always lock them in the same order, one way you could decide the order is on the results of System.identityHashCode(Object)

References:
http://stackoverflow.com/questions/1636399/correctly-synchronizing-equals-in-java
http://download.oracle.com/javase/tutorial/essential/concurrency/atomic.html


Never abruptly exit a finally block

Every finally block should complete normally, barring an unchecked exception.
Never exit a finally block with a return, break, continue, or throw, and never allow a checked exception to propagate our of a finally block.
When both try block and the finally block complete abruptly, the reason for the abrupt completion in the try block is discarded. Discarding the reason for abrupt completion is almost never what you want ... (Java Puzzlers p.78)


References:
http://accu.org/index.php/journals/236 (handle checked exception in finally block)
http://www.javamex.com/tutorials/exceptions/exceptions_finally.shtml
http://www.javamex.com/tutorials/exceptions/exceptions_hierarchy.shtml

Wednesday, December 8, 2010

Install VLC on CentOS

If you are not able to install VLC through command "yum install vlc" then most likely you have to enable the RPMForge repository by following the instructions provided by the link here

p.s. RPMforge is a collaboration of Dag and other packagers. They provide over 5000 packages for CentOS, including wine, vlc, mplayer, xmms-mp3, and other popular media tools. It is not part of Red Hat or CentOS but is designed to work with those distributions.

Java volatile vs. synchronized

If volatile already synchronizes data across threads, what is synchronized for? Well there are two differences. Firstly synchronized obtains and releases locks on monitors which can force only one thread at a time to execute a code block, if both threads use the same monitor (effectively the same object lock). That's the fairly well known aspect to synchronized. But synchronized also synchronizes memory. In fact synchronized synchronizes the whole of thread memory with "main" memory.

volatile only synchronizes the value of one variable between thread memory and "main" memory, synchronized synchronizes the value of all variables between thread memory and "main" memory, and locks and releases a monitor to boot. Clearly synchronized is likely to have more overhead than volatile.

volatile:
- Get a global lock on the variable
- Update the one variable from main memory
- Write any change of the one variable back to main memory
- Release the lock

synchronized:
- Get a global lock on the monitor
- Update all shared variables that have been accessed from main memory
- Process some statements
- Write all shared variables that have been changed back to main memory
- Release the lock


reference: http://www.javaperformancetuning.com/news/qotm051.shtml

Saturday, December 4, 2010

How to record/replay X events

Download xnee 2.05
> cnee --record --mouse --keyboard --out-file mouse-keyboard-events.xnl
> cnee --replay --file mouse-keyboard-events.xnl

Run shell script through keybinding

Press Ctrl+Alt+Insert to start an Xterm running the 'top' command, and press Ctrl+Alt+Delete to kill it:
gconftool-2 -t str --set /desktop/gnome/keybindings/custom1/name "Start top command"
gconftool-2 -t str --set /desktop/gnome/keybindings/custom1/binding "Insert"
gconftool-2 -t str --set /desktop/gnome/keybindings/custom1/action "xterm -e /usr/bin/top"

gconftool-2 -t str --set /desktop/gnome/keybindings/custom2/name "Stop top command"
gconftool-2 -t str --set /desktop/gnome/keybindings/custom2/binding "Delete"
gconftool-2 -t str --set /desktop/gnome/keybindings/custom2/action "xterm -e kill -9 $(ps aux grep top grep -v grep)"