Tuesday, January 22, 2013

Me


This is me.
The author is about 2,5 years old :-)

Friday, January 4, 2013

PrivateStaticVolatile

JeeProjectSnippets class is a simple Singleton implementation. This example demonstrates when is useful to use volatile keyword for the creation of an instance in Java. I use volatile variable in this example to ensure that every thread can see already updated value of instance:


package com.jeeprojectsnippets.serialization;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class JeeProjectSnippetsTest {

    public static void main(String[] args) throws IOException,
            ClassNotFoundException {

        JeeProjectSnippets singleton = null;

        for (int i = 1; i < 6; i++) {
            singleton = JeeProjectSnippets.getInstance();

            File file = new File("ser" + i + ".txt");

            ObjectOutputStream oos = new ObjectOutputStream(
                    new FileOutputStream(file));
            JeeProjectSnippets.a++;
            singleton.b++;
            JeeProjectSnippets.c++;
            singleton.d++;
            JeeProjectSnippets.f++;

            oos.writeObject(singleton);
            oos.close();

            ObjectInputStream ois = new ObjectInputStream(
                    new FileInputStream(file));
            JeeProjectSnippets jps = (JeeProjectSnippets) ois.readObject();

            System.out.println(i + ".\na_static: " + JeeProjectSnippets.a);
            System.out.println("b_transient: " + jps.b);
            System.out.println("c_static_volatile: " + JeeProjectSnippets.c);
            System.out.println("d_volatile: " + jps.d);
            System.out.println("e_final_static: " + JeeProjectSnippets.e);
            System.out.println("f_static_transient: "
                    + JeeProjectSnippets.f + "\n\n");
            ois.close();

        }
    }

    /**
     * JeeProjectSnippets class is a simple Singleton implementation. This
     * example demonstrates when is useful to use volatile keyword in Java. I
     * use volatile variable in this example to ensure that every thread can see
     * already updated value of instance.
     */
    public static class JeeProjectSnippets implements Serializable {

        private static final long serialVersionUID = 4408143805430443067L;

        private static int a = 10;
        private transient int b = 20;
        private static volatile int c = 30;
        private volatile int d = 40;
        public final static int e = 50;
        private static transient int f = 60;

        private static volatile JeeProjectSnippets instance;

        public static JeeProjectSnippets getInstance() {
            if (instance == null) {
                synchronized (JeeProjectSnippets.class) {
                    if (instance == null)
                        instance = new JeeProjectSnippets();
                }
            }
            return instance;
        }
    }
}

Testing output of transient and static volatile variables:
1.
a_static: 11
b_transient: 0
c_static_volatile: 31
d_volatile: 41
e_final_static: 50
f_static_transient: 61


2.
a_static: 12
b_transient: 0
c_static_volatile: 32
d_volatile: 42
e_final_static: 50
f_static_transient: 62


3.
a_static: 13
b_transient: 0
c_static_volatile: 33
d_volatile: 43
e_final_static: 50
f_static_transient: 63


4.
a_static: 14
b_transient: 0
c_static_volatile: 34
d_volatile: 44
e_final_static: 50
f_static_transient: 64


5.
a_static: 15
b_transient: 0
c_static_volatile: 35
d_volatile: 45
e_final_static: 50
f_static_transient: 65