Monday, March 28, 2011

JQTJSF: Java-quick-thread-job-splitting/forking

(Sorry about the stupid title but "laws of nature" force me to prefix anything related to Java with an annoying, uggly, empty of content and counter-intuitive acronym).

Reading about closures I found the next quite useful paragraphs (Copy&Paste from the Wikipedia):

Java allows defining "anonymous classes" inside a method; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.
class CalculationWindow extends JFrame {
private volatile int result;
public void calculateInSeparateThread(final URI uri) {
// The expression "new Runnable() { ... }" is an anonymous class.
new Thread(
new Runnable() {
void run() {
// It can read final local variables:
// It can access private fields of the enclosing class:
result = result + 10;

Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container.

According to a Java 7 proposal[11], closures will allow the above code to be executed as:
class CalculationWindow extends JFrame {
private volatile int result;
public void calculateInSeparateThread(final URI uri) {
// the code #(){ /* code */ } is a closure
new Thread(#(){
calculate( uri );
result = result + 10;

Friday, March 18, 2011

Limiting Flash CPU usage in Linux (Firefox only)

Firefox makes it easy to enable/disable plugins like Flash.

The problem is that there are many webs out there. Some of them use Flash legitimately and with good intentions, while some others try to invade us with ads banners without taking a minimal effort to keep resource usage low. Switching on/off the flash plugin is not always an option or at least not a confortable one. Ideally it would be great to have Flash running while simultaneously get certain that it will not peak CPU usage slowing down web navigation.

Newer versions of Firefox use an external process named "plugin-container" to execute external plugins like Flash. Fortunately that makes it quite easy to control the maximum CPU percentage we want to assign to Flash. All we need is to follow the next 2 steps:

- Install the command line utility "cpulimit". How to do it must be straightforward if its already in our favority linux distro repository.

- Edit (as root) /etc/rc.local and add a line similar to:

cpulimit -l 20 -e plugin-container &

Once we reboot our system 'cpulimit' will take care that external firefox plugins (aka flash) don't get more than 20% of our CPU. That can be too slow for people who want to check videos on youtube, so they will probably want to use a higher percentage. 20% is just a personal option. (Don't forget also that VLC allows to play youtube videos and it performs much better than the Flash plugin:

In my particular system, a "not so 3-years-old" laptop, I'm used to set cpu frequency to the minimum available clock, 800Mhz. That let's me use the laptop as a real LAPtop, I mean without my laps being burn by the heat dissipated by the CPU. Before using cpulimit I had no other option that turning off the plugin flash since randomly some "aggresive" flash banner knocked down my web browser. After cpulimiting the plugin those banners will need to manage themselves how to share the 20% cpu left for them. Not my problem anymore.

What about Chrome? I think, for what I have read about, that Google Chrome is ussing some type of plugin container too, but I found no easy way to control it. "top" just show a single process for the browser and the plugin so I didn't find any easy way to let cpulimit control it. Any hint/idea is wellcome!

Friday, March 11, 2011

God save the VIM!: My personal .vimrc

set number            <-- A must 
set ignorecase        <-- quite useful
set encoding=utf-8
set tabstop=4          
set shiftwidth=4      <-- 8 is too much and annoying
set expandtab         <-- replace tabs with spaces. That makes layout independent of tab's setup.
set foldmethod=indent <-- Ummm, depends on taste. Automatic folding based on indentation
Once a file is open it may be better to change it to foldmethod=manul
set cursorline        <-- Visual help (cursorcolumn is useful sometimes) 
syntax off            <-- Syntax highlighting never works to me.
set cindent           <-- C indent rules                                           
set smartindent       <-- Make vim smart about indentation
set autoindent        <-- Enable autoindent 
" associate file extension (lp) with syntax (lua)
au BufNewFile,BufRead *.lp setlocal ft=lua
" shorcut for if-then-else in C-style languages
ab ite 
\if () {
\} else if () {
\} else if () {
\} else {
\} end  

" shorcut for if-then-else in lua
ab itel 
\if ()  then -- {
\elseif () then -- } { 
\elseif () then -- } { 
\else -- } { 
\end -- }

fu! SaveSess()
    execute '!mkdir .vim'
    execute 'mksession! .vim/session.vim'

fu! RestoreSess()
execute 'so .vim/session.vim'
if bufexists(1)
    for l in range(1, bufnr('$'))
        if bufwinnr(l) == -1
            exec 'sbuffer ' . l

autocmd VimLeave * call SaveSess()
autocmd VimEnter * call RestoreSess()
" indentation rules for FFmpeg
set shiftwidth=4
set softtabstop=4
"set cindent         < -- useful for C alike languages
"set cinoptions=(0   < -- useful for C alike languages

autocmd FileType make,automake set noexpandtab shiftwidth=8 softtabstop=8 < -- " Allow tabs in Makefiles.
highlight ForbiddenWhitespace ctermbg=red guibg=red < -- hihglight forbidden Trailing whitespace and tabs
match ForbiddenWhitespace /\s\+$\|\t/
autocmd InsertEnter * match ForbiddenWhitespace /\t\|\s\+\%#\@<!$/ < -- Do not highlight spaces at the end of line while typing on that line.
Update 2012-04: The RestoreSess/SaveSess (Grotty copy&paste from is a really great addition. It saves and automatically restore all the vim session (open file, tabs, window layouts,...). One more reason to say Eclipse "Good Bye!".

Update 2014-05: Unashamedly copied Makefile related stuff from ffmpeg documentation project.

Update 2014-11:  associating file extensions with language syntax and shorcuts for if-then-else.

External Links: