Monday, January 12, 2015

Synchronized list and static member class in Java Program



How to create synchronized list

package com.java2novice.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MySyncList
{
public static void main(String a[])
{
        List li = new ArrayList();
        List sysLi = Collections.synchronizedList(li);
        System.out.println("Synchronized list got created...");
    }
}

How to create synchronized set

package com.java2novice.collections;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class MySyncSet
{
public static void main(String a[])
{
        Set ss = new HashSet();
        Set sysSet = Collections.synchronizedSet(ss);
        System.out.println("Synchronized set got created...");
    }
}

How to create synchronized map

package com.java2novice.collections;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class MySyncMap
{
public static void main(String a[])
{
        Map mp = new HashMap();
        Map sysMapt = Collections.synchronizedMap(mp);
        System.out.println("Synchronized map got created...");
    }
}

How to initialise or call a static member class

package com.java2novice.nested.classes;
public class MyBasicStaticMemberClass
{
public static class MyStaticMemberExampleClass
{
public void printStatus()
{
            System.out.println("Hey I am inside static member class");
        }
    }
public static void main(String a[])
{
        StaticMemberTestClass smt = new StaticMemberTestClass();
        smt.testMemberClass();
    }
}
class StaticMemberTestClass
{
public void testMemberClass()
{
        MyBasicStaticMemberClass.MyStaticMemberExampleClass msme
                    = new MyBasicStaticMemberClass.MyStaticMemberExampleClass();
        msme.printStatus();
    }
}

No comments:

Post a Comment