Thursday, July 31, 2008

Thread Local

Thread local is the concept of storing data which is specific to the thread. For example member variables are specific to instance, thread local variables are specific to Thread.

The IBM's page outlines exactly how it works..

This is definitely good way to do thread related transactions where in each thread denotes on transaction or session. Caling the get method on the thread local will return value based on the thread from which the method is called.

An example:

public class ThreadLocalTest {

static class CountdownThread extends Thread {
public static final ThreadLocal local = new ThreadLocal();
private int start;

public CountdownThread(int s) {
start= s;
}

public void run() {
init();
while(true) {
System.out.println(local.get());
int i = local.get();
local.set(--i);
if(i==0) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void init() {
local.set(start);
}
}

public static void main(String[] args) {
CountdownThread t1 = new CountdownThread(10);
CountdownThread t2 = new CountdownThread(5);
t1.start();
t2.start();

}
}


Output:

10
5
4
9
3
8
2
7
1
6
5
4
3
2
1

I wonder how come I didn't hear about this for long time!!!

Happy coding!!

Wednesday, July 23, 2008

Open source licenses...

Open source has been the buzz word for sometime.. Whether you like it or not, we have to use open source products in our business today and so do we in our company. If we don't have a proper understanding of different open source licenses, you may put your company/product into trouble. So all software developers have to take this training and this training was very useful and informative. So here is what I learnt from it...
  • Open source and free/shareware are completely different. They are NOT same.
  • Open source coined by FSF (free software foundation), refers to sharing of source code.
Various open source licenses:

GPL: GPL stands for General Public License and it is most popular license in open source community. If you use a software which is GPL licenses in your code, you just have to share your code as well as whatever improved you do to the GPL license code. There are various versions of GPL, GPL 3.0 being the latest and most stricter of all.. If you use GPL code in your job, we really have to be careful!

MPL: Mozilla public license. It is less stricter than GPL, but more stricter than BSD. If is also called "Patent peace" license. This license strongly discourages patent infringement fights between parties

EPL : Eclipse public license. Less stricter than MPL and very business friendly open source license. If you proprietary codes just uses the API/libraries provided by EPL license, you don't have to disclose you proprietary source code! Good to do business, isn't it!!

CPL: Common public license. Created by IBM and also used by Microsoft. This license permits you to enhance a CPL licensed code and release the binaries for stricter license. Another big difference is that, contributors cannot be anonymous. they must identify themselves.

CDDL: Very similar to CPL

MIT License: Permissive license. You can do whatever you want with the source code, as long as you distribute the MIT license with your code.

BSD: Very similar to MIT license

Another important things is also related to how you use the 3rd party libraries. For example, if you use 3rd party libraries through sockets, pipes it is different from directly calling the API proprietary code, and this was a suprise to me!!

Treat the above as just an information. Please verify and read each individuals license if you need exact information.