Saturday, May 27, 2017

Why volatile is not enough?

When thread uses some variable, JVM optimizes variable read/write by caching that value in thread local space, but this gives rise to unique problem Data Inconsistency. Lets see how



As from above image there are 2 threads, each using same variable, Now JVM copies that variable to thread local storage, Now lets say thread 1 modifies it, now thread 2 has no way to see that change as it already has its own copy of variable. So by using volatile we tell JVM that don't create local copy of variable instead let them use the original variable.

Now is this enough? I mean just using volatile all read / writes to variable will be correct or variable will be in consistent state, well NO, volatile only guarantees consistency in READS not WRITES or in other word it only guarantees Happens-Before relationship.

So How we gain both Read And Write consistency?

1) Synchronized / Lock 

We can use synchronized access to variable by Synchronized block or By using lock object
But this yields performance impact.

2) Atomic Wrappers

 Newer version of Java has Atomic Wrappers for primitive types like AtomicInteger which guarantees Atomic Read/write to the variable in lock free manner so this is much faster than Synchronized  or Locks.



Sunday, May 31, 2015

Tricks to Save Internet Data Usage

Internet is part of our lives and this Internet isn't cheap at all. All over the world prices for Internet are rising & especially on mobile internet, data is so much costly.

Here are some ways to save some data usage.

Browsers : 
If you are doing heavy browsing, try using browsers that supports page compression & Image compression
eg. Opera browser's Turbo Mode

Downloads:
Other than videos,Images,audio:
If you are downloading documents,files then first try to compress them online by tools like CloudConvert.
eg. If you need to download file of 100MB -> Get it compressed by CloudConvert (7z has highest compression ratio) & download compressed one. I am sure you will save atlest 20-30MB's on this.

Use Download Managers;
If you are downloading big files try to use download managers like Free Download Manager, because many built in download managers in popular browser dont have proper download resuming capabilities on download failure. You can also add multiple peer locations(mirrors) to speedup download.

Many times download site has heavy load & even using downloaders you may endup in currupted download, in this case first convert that download to torrent by "Burnbit" & download that file by using torrent. So evenif some chunks of file gets currupted torrent will altomatically redownload it. This will save your re-download attempt.

Torrent Movie Downloads:
Now instead of downloading avi,mp4 format movies, try to download movies in mkv format.
Internally they use different video,audio codecs & compression schemes this makes mkv files considerably smaller than that of mp4 or avi file
eg. avi,mp4 file of 1GB may have same audio-video quality in 500-700MB mkv file

YouTube Downloads:
Instead of downloading youtube videos in mp4 or avi format, try to download them in webm or mkv format
You will have smaller filesize with same quality as mp4,avi video.

So to save some data on video-audio files try to download video compressed with VP8,VP9 Codecs & audio with vorbis audio.