Home     RSSRSS

Concurrency, Thread Safety and Local Variables

August 12, 2016 by kiranbadi1991 | Comments Off on Concurrency, Thread Safety and Local Variables | Filed in Development, Performance Center, Performance Engineering, Performance Test Tools

One of the most of the common reason for concurrency issues I often see in web application is due to concurrent access of data stored in variables. Generally in servlets , data in variables are often stored as Local variables, Instance Variables, Class Variables , request attributes, session attributes and context attributes.

Below example simplest I can think of for storing data as local variable and accessing it in a thread safe manner

public class MyServlet extends httpServlet {

  // mylocalage is localvariable here for this servlet.

    public void printAge(){
              int mylocalage = 0;
      mylocalageage = mylocalage - 10;
      System.out.println("My age 10 years earlier was: " + mylocalage);
      

    }
}

Its considered that by design that data stored in local variable is thread safe.

Every thread accessing the above servlet will have their own values and they will not interface with each other.

image

Local variables are stored in stack in Java. So data stored in these variables are thread safe.

Tags: , ,

Java – Accept Input String and Return array of Hashmaps.

February 14, 2016 by kiranbadi1991 | Comments Off on Java – Accept Input String and Return array of Hashmaps. | Filed in Development

Recently I came across one of the exciting interview question,write the method that accepts input string and returns array of hashmaps.

Below are my 2 attempts, first one is somewhat wrong and second one is very close to correct but bit of incomplete.


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author kiran
 */

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 
 Write a method for parsing strings. Input is a string and then return an array of hashmaps.
 
 Input = "This is a test. And this is another test."
 Return = [{1=>"This", 2=>"is"..},{1=>"And", 2=>"this"...}..]
 
 */
public class ParseString {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your Input String: ");
        String Input = scanner.nextLine();
   //     String Input = "This is a test. And this is another test.";
        List<Map<Integer, String>> list = new ArrayList<>();
        int length = Input.length();
        StringTokenizer st = new StringTokenizer(Input);
        while (st.hasMoreElements()) {
            length++;
            for (int i = 0; i <= length; i++) {
                int key = i;
                String value = st.hasMoreTokens() ? st.nextToken() : null;
                if (value != null) {
                    Map<Integer, String> map = new HashMap<>();
                    if (!map.containsKey(key)) {
                        map.put(key, value);
                    }

                    System.out.println("key : " + key + " value : " + value);
                    list.add(map);
                }
            }
        }
        System.out.println("List is : " + list);

    }

}




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 Read the rest of this entry »

Implementing Caching Solution

April 28, 2015 by kiranbadi1991 | Comments Off on Implementing Caching Solution | Filed in Development, Environment, Performance Engineering

I have been in quite a few engagements where projects used in process caching and distributed caching using various open source and commercial products.Some projects used memcache,ehcache and some used oracle coherence.I have seen project’s implement in different ways.Caching can be implemented declaratively or programmatically and some of the common questions to ask while implementing cache is,

– How do we refer to the cache component, by name , or id or something else .
– How many items do we need to store in cache memory
– How long do we want to store the items in cache
– Do we need to store items to disk as well, if yes how many items

I think these questions are good enough to get started on cache implementation.However depending on projects requirements we can implement caching in different ways.Maybe I will write something about it in next post.

Tags: , , ,