As we work, we invent new tools, new ideas. Being associated with this "Information technology / Software Industry" for about 5 years now, would like to share a few tools and ideas, developed out of interest or needs. Therefore this blog.

Saturday, April 4, 2009

Watch Your Tongue!

Serialization :  When being asked, what is SERIALIZATION, I get answers which are really annoying. The following are some samples :)
  • "The process of writing the state of an object to a file!"
  • "Saving the object's state to the file system!"
  • "Writing the state of an object to a file using streams basically output streams!"
Only to a file? What about saving the object's state in a database? Will saving in database not involve 'Serialization'? The above answers are more ridiculous than they actually seem.

Big question, what is it? The following is a closer one to what actually it is :

"Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time."  
                               (- Extract from Sun's Technical Article)

     So, where is this 'serialization' needed? The answer is simple when we write any information to the disc, we write as bytes or when we transfer information via wire to machines, we transfer as bytes. Hence, to save an object's state or to transfer an object's state via wire, we need to express the object's state as a sequence of bytes.

Also at the receiver's end we need to construct some meaningfull information out of those bytes, or when we re-read the bytes from the storage device. Hence the whole process of serialization.

     We should not encourage the use of 'de-serialization', because the whole process of state to bytes and then bytes to state gives it a complete meaning, which is serialization.

Transaction: What do we mean by a TRANSACTION? comming soon...


Hello World!

Could we write a simple program to continously print "Hello... ...World" in a single line, but "Hello... " and "...World" being print by different threads? This question was asked to me by one of my collegue (thanks to Saikat Manna).

Here is as an attempt (pardon me Saikat, is anything wrong! :D) :

import static java.lang.System.out;

/**
 * @author Kaushik Roy
 *
 */
public final class HelloWorldThread {
private static volatile String state = "HELLO";
/**
* @author Kaushik Roy
*
*/
static class HelloThread implements Runnable {
private Object  mutex;
/**
   * @see java.lang.Runnable#run()
*/
@Override
public void run() {
while (true) {
if ("HELLO".equals(state)) {
synchronized (mutex) {
out.print("Hello ... ");
state = "WORLD";
try {
mutex.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
  }
/**
* @param mutex
*/
public HelloThread(Object mutex) {
this.mutex = mutex;
}
  }
/**
* @author Kaushik Roy
*
*/
static class WorldThread implements Runnable {
private Object          mutex;
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
while (true) {
if ("WORLD".equals(state)) {
synchronized (mutex) {
out.println(" ... World");
state = "HELLO";
mutex.notify();
}
}
}
}
/**
* @param mutex
*/
public WorldThread(Object mutex) {
this.mutex = mutex;
}
}
/**
* @param args
*/
public static void main(String[] args) {
Object mutex = new Object();

new Thread(new HelloThread(mutex)).start();
new Thread(new WorldThread(mutex)).start();
}
}

Hope this works! Here goes the sample 'output':

Hello ...  ... World
Hello ...  ... World
Hello ...  ... World
Hello ...  ... World
Hello ...  ... World
Hello ...  ... World
Hello ...  ... World
Hello ...  ... World
Hello ...  ... World

Nothing great about the output. This is a very simple example illustrating the use of wait( ) and notify( ) methods with a mutex. Sun tutorial's Producer-Consumer example is a better one.

PS: This article is for Kiran Wali.

Followers

Archives