Jeetu Alex - IT Software Professional specialized in developing high-end, customized business solutions using Coldfusion and Java.

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

11 comments :

Aashi siva said...

This idea is mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing this explanation.Your final conclusion was good. We are sowing seeds and need to be patiently wait till it blossoms.

Corporate Training in Chennai

Sathya G said...


Thanks for this selfless act. really feel very happy about sharing your useful update with us.
ROI Services in Chennai

Giri Mani 2 said...

This blog is having a wonderful talk. The technology are discussed and provide a great knowledge toall. This helps to learn more details about technology. All this details are important for this technology. Thank you for this blog.
Digital Marketing Company in Chennai

Sowmiya said...

your java related concepts and interview questions are really helpful and it is very much interesting as well as i like your blog because you have done a great job. thanks for posting.



Best Android Training Institute in Chennai

for IT the said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

for IT the said...

Java Training Institutes Java Training Institutes Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Spring Training Institutes in Chennai Spring Training Institutes in Chennai

for IT the said...

Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online

Jeffy said...

Thank you for sharing such a nice and interesting blog with us. Hope it might be much useful for us. keep on updating...!!
seo company in india
digital marketing company in india
seo company in chennai
digital marketing company in chennai

Shalini said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.

seo company in india

Ramya Krishnan said...

Thanks for your article, it gives clear picture to understand.
Java Training in chennai

trustno1 said...

core java programming examples
java example| Recursive factorial method

Post a Comment