Friday, January 25, 2013

HashTable V/s HashMap


HashTable -> synchronized – is a legacy class , Enumeration in the Hash table is not fail-fast
-            
HashMap -> not synchronized, Itrator in the hashmap is fail-fast
                        We can make the hashmap synchronized when ever required using Collections.SynchronizedMap(map)
                        Does not garentee the order of the keyvalue pair in the entered order, but the subclass of hashmap, which is LinkedHashMap we can achieve this.

Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

Fail-fast
A fail-fast system is designed to immediately report any failure or condition that is likely to lead to failure. Fail-fast systems are usually designed to stop normal operation rather than attempt to continue a possibly-flawed process. When a problem occurs, a fail-fast system fails immediately and visibly. It will sounds like making your software more fragile, but it actually makes it more robust. Bugs are easier to find and fix, so fewer go into production

When to use what?
Whenever there is a possibility of multiple threads accessing the same instance, we should use Hashtable. While if not multiple threads are going to access the same instance then use HashMap. Non synchronized data structure will give better performance than the synchronized one.
Again , may be later point of time - there can be a scenario when you may require to retain the order of objects in the Collection with key-value pair then HashMap can be a good choice. As one of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you can easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
Also if you have multiple thread accessing you HashMap then Collections.synchronizedMap() method can be leveraged.

Verdict:-Overall HashMap is better in all aspects.

HashMap Sample Program:-

class HashMap
{
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();

// Put elements to the map
hm.put("Sujith", new Double(71.5));
hm.put("Manjusha", new Double(53.6));
hm.put("Sukhesh", new Double(78.5));
hm.put("Ranjith", new Double(81.1));

// Get a set of the entries
Set set = hm.entrySet();

// Get an iterator
Iterator i = set.iterator();

// Display elements
while(i.hasNext())
{
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
}
}


mysqlbinlog : Enable binary logs in mysql and extract from bin file

MySQL Server generate binary log files for every db transaction, provided administrator does not disable. The binary log files are written in binary format. It will be stored in /var/lib/mysql directory. It cannot be read directly, as it is in binary format. So we need to use mysqlbinlog command to read in text file.

Check enabled?


SELECT * from information_schema.GLOBAL_VARIABLES WHERE VARIABLE_NAME = 'LOG_BIN';
 Or
SELECT @@log_bin;
Or
SHOW VARIABLES LIKE 'log_bin';
If not enabled:-enable binary logs in mysql

Add this to /etc/my.cnf:

log-bin=mysql-bin

[root@rhel6 ~]# /etc/init.d/mysqld restart --log-bin
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@rhel6 ~]# mysql -u root -p
Enter password:

mysql> show binary logs;
ERROR 1381 (HY000): You are not using binary logging
[root@rhel6 ~]# updatedb
[root@rhel6 ~]# locate mysql-bin

Mysql - To extract from bin file:

mysqlbinlog  is a tool to analyze and view the binlogs  from mysql, which are stored in binary format. This will converts them to plaintext, so that  it can be readable.

--start-datetime and --stop-datetime , both will accept DATETIME or TIMESTAMP entries, and which together set the start/stop of what kind of information we’re interested in.
Ex:-
mysqlbinlog --start-datetime="2012-09-05 00:00:00" --stop-datetime="2012-12-31 14:00:00" mysql-bin.000019 --result-file=05Sept_31Dec.txt

mysqlbinlog --start-datetime="2012-12-31 01:00:00" mysql-bin.000019 --result-file=31dec.txt

Monday, January 14, 2013

REST and SOAP Web Services


REST Web Service

  • REST => Representational State Transfer,  means  each unique URL is a representing some object. You can get the contents of that object using an HTTP GET, also you can use a POST, PUT, or DELETE to modify the object.
  • It is Light weight ,  not a lot of extra xml markup, Human Readable Results,  Easy to build
  • REST has no WSDL interface definition
  • REST is over HTTP, REST is stateless
  • REST can be  plain text, JSON, HTML etc which can transfer over HTTP
  • REST services are easily cacheable.
  • In REST, For transport security we can use https and for authentication, basic auth
  • REST is much simpler and easy to interop in many languages.
  • REST is a better choice for integration between websites, with public API, on the TOP of layer (VIEW, ie, javascripts taking calls to URIs).

SOAP Web Service:

  • SOAP is a protocol.
  • SOAP was designed to give access to Objects as in Object Oriented Programming Objects; i.e., data plus methods
  • SOAP can be over any transport protocols such HTTP, FTP, STMP, JMS
  • SOAP is using soap envelope
  • From any given WSDL, we can generate
  • The problem with SOAP is its complexity when the other WS-* specifications come in and there are countless interop issues if you stray into the wrong parts of WSDL, XSDs, SOAP, WS-Addressing etc.
  • SOAP is a XML-based protocol that tunnels inside an HTTP request/response, so even if you use SOAP, you are using REST too
  • SOAP is a better choice for integration between legacy/critical systems and a web/web-service system, on the foundation layer, where WS-* make sense (security, policy, etc.).



Thursday, October 4, 2012

Windows 7 Sound issue- error 1722 recovered


Problem

I Lost my sound- Got the below error when starting services->windows audio endpoint builder

 error 1722 the rpc server is unavailable

My solution

For some reason my "Power" service was off .
Start the "Power" service and then start "windows audio endpoint builder" and" windows audio" service -  now my audio services started and I got my sound again back. 



Saturday, June 16, 2012

Donate Blood, Save Life


Type
Donate to
Receive from
A+
A+ , AB+
A+ , A- ,O+ , O-
A-
A+ , A- , AB+ , AB-
A -, O-
O+
O+ , A+ , B+ , AB+
O+ , O-
O-
EVERYONE
O-
B+
B+ , AB+
B+ , B- , O+, O-
B-
B+, B- AB+ , AB-
B- , O-
AB+
AB+
EVERYONE
AB-
AB+ , AB-
AB- , A- , B-, O-


Blood bank Url's

  • http://www.bharatbloodbank.com/
  • http://www.indianblooddonors.com/
  • http://www.blooddonorsindia.org/
  • http://www.sakshum.org/ui/page/FindDonor.jsp



Thursday, June 14, 2012

Java Interview questions

Let me note down the questions which i faced so far:- wl discuss more in detail one by one:-


  • Write code for singleton pattern/Factory Pattern
  • Synchronizhation is better in method or block in this case?
  • Write code to validate ip address
  • write code to retrive data from vector/arraylist in perticular index
  • Write code for add and retrive data from hashmap
  • Internal storing and retrival of hashmap( equals and hashcode? )
  • Exception hirarchy?
  • How to write validater.xml if 50000 records retived has to validate
  • Flow/architecture of the application currently working
  • Call back function in ajax
  • can we call action class from ajax
  • call back is sync/ acsynch ?
  • Explain concept of ENUM
  • Explain How Hashmap works internally
  • Different types of servlet
  • What happend to transient field after deserialization
  • Externalizable
  • How you extract and sort a string having repeated words in it
  • How the transient variable object will be restored in deserilsation?- null or default for premitives
  •  How marker interfaces are interpreted by JVM(serializable,clonable), as it doesn't hv any method implementation 
  • Why IT industry is down mainly the telecom now?
  •  Priority of Thread?
  • How java do memory management in java?
  • when servlet will call destroy()
  • How to manage with session if the session size is large
  • Http is sateless/how session tracking?
  • Why String class is final?
  •  Can a class be static?
  • wht is difference b/w abstract class?or why abstract class as we can do it with out abstract also?
  • Explain singleton and loop holes of singleton and how to overcome


Friday, March 16, 2012

Load Table from a file

Load a file with tab seperated to a table

filename.txt

col1_data1 col2_data1 col3_data1 col4_data1 col5_data1
col1_data2 col2_data2 col3_data2 col4_data2 col5_data2


load Table Table_name (
col1,
col2,
col3,
col4,
col5
)
FROM '/home/sujith/filename.txt'
QUOTES ON
ESCAPES OFF
FORMAT BCP
STRIP OFF


col1 col2 col3 col4 col5
col1_data1 col2_data1 col3_data1 col4_data1 col5_data1
col1_data2 col2_data2 col3_data2 col4_data2 col5_data2