Divide and Conquer by Java Threads

Suppose you have task which collects the data the from various sources and creates a Object containing all this information e.g. information is distributes over various databases, websites and some you need to calculate based on that data. The single threaded application works fine with small amount of data however as data starts growing single threads shows its limitation.

There is a interesting property of threads which comes to our rescue. Thread maintains the private copy of object on which it operates called as working copy in which it keeps the local modification which are made to the object. While main memory contains the master copy of every object.

After all threads stop executing all the working copies are merged into the master copy. This becomes the curse in case of multiple objects modify the same field in the object because merging back creates the confusion and results in a unpredictable behavior. However if each thread works on the different field of the object merging won't cause a conflict and hence state remains consistent and predictable.

This improves the performance many folds and hence application gain scalability which was missing in the single threaded application.

Snippet:

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        final List list = new ArrayList();
        Person person = null;
        for (int i = 0; i < 100; i++) {
            person = new Person();
            Runnable runnable = createFirstRunnable(person);
            Runnable run = createSecondRunnable(person);
            Thread thread = new Thread(runnable);
            Thread tt = new Thread(run);
            thread.start();
            tt.start();
            list.add(person);
        }

        for (Person p : list) {
            System.out.println("This is " + p.getFirstName() + " " + p.getLastName() + "'s Blog.");
        }
    }

    private static Runnable createSecondRunnable(final Person person) {
        Runnable run = new Runnable() {

            @Override
            public void run() {
                person.setLastName("Gupta");

            }
        };
        return run;
    }

    private static Runnable createFirstRunnable(final Person person) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                person.setFirstName("Subodh");
            }
        };
        return runnable;
    }

    private static class Person {
        private String firstName = null;
        private String lastName = null;

        public Person() {
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
}

Comments

Popular posts from this blog

java.net.SocketException: Connection reset

Hide empty last column from Table/Tree Viewer

Spring: implements interface not working in @Controller