Tech Rocks

Coldfusion
Java
JQuery

An online resource for latest web technologies like Coldfusion, JRun, Pro*C, JQuery, HTML5, PHP, W3C, Java, J2EE, C, C++, ORACLE, PL/SQL, MySql, Ajax, Coldbox, Fusebox, UNIX, JavaScript, NodeJS and much more...

Tuesday, July 5, 2016

Collections & Threads in Java

package com.jeetu.app.util;

import java.util.*;

public class test {
    public static void main(String[] args) {

        List linkedList = new LinkedList();
        executeLogic(addContents(linkedList, "LinkedList"), displayContents(linkedList, "LinkedList")).start();

        List arrayList = new ArrayList();
        executeLogic(addContents(arrayList, "ArrayList"), displayContents(arrayList, "ArrayList")).start();

        Set hashSet = new HashSet();
        executeLogic(addContents(hashSet, "HashSet"), displayContents(hashSet, "HashSet")).start();

        Set treeSet = new TreeSet();
        executeLogic(addContents(treeSet, "TreeSet"), displayContents(treeSet, "TreeSet")).start();

    }

    static Thread executeLogic(final Thread t1, final Thread t2) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        t1.start();
                        t1.join();
                        t2.start();
                        break;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return t;
    }

    static Thread addContents(final Collection e, final String type) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                long end;
                long start = System.currentTimeMillis();
                long limit = 5000000;
                for (int index = 1; index <= limit; index++) {
                    e.add(index);
                }
                end = System.currentTimeMillis();
                System.out.println("Write[" + type + "]: " + (end - start) + "  ms.");
            }
        });
        return t;
    }

    static Thread displayContents(final Collection e, final String type) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                long end;
                long start = System.currentTimeMillis();
                int total = 0;
                Iterator i = e.iterator();
                while (i.hasNext()) {
                    total = total + (Integer) i.next();
                }
                end = System.currentTimeMillis();
                System.out.println("Read[" + type + "]: " + (end - start) + " ms ,Total = " + total + ".");
            }
        });
        return t;
    }
}

Result:
Write[ArrayList]: 12997  ms.

Write[LinkedList]: 13144  ms.

Read[ArrayList]: 185 ms ,Total = 1647668640.

Read[LinkedList]: 69 ms ,Total = 1647668640.

Write[HashSet]: 29986  ms.

Read[HashSet]: 69 ms ,Total = 1647668640.

Write[TreeSet]: 38823  ms.

Read[TreeSet]: 67 ms ,Total = 1647668640.



Process finished with exit code 0