Sunday, June 12, 2016

Intersection point of two linkedlist



public class LinkedListYShape {

     static Node head1, head2;

     public static void main(String[] args) {
           Node node1 = new Node(3);
           Node node2 = new Node(6);
           Node node3 = new Node(9);
           Node node4 = new Node(15);
           Node node5 = new Node(30);
           Node node6 = new Node(10);
           node1.setNextNode(node2);
           node2.setNextNode(node3);
           node3.setNextNode(node4);
           node4.setNextNode(node5);
           node6.setNextNode(node3);

           // Creating first and second Linked List
           LinkedListYShape list = new LinkedListYShape();
           list.head1 = node1;
           list.head2 = node6;
          
           // Approach 1-(Using difference of node counts)
           System.out.println("Intersection Node is " + list.getNode());
          
           // Approach 2-(Simply use two loops)
           System.out.println("Intersection Node is "+                 list._getIntersectionNodeUsingLoop(head1, head2));
     }

     int getNode() {
           int length1 = getCount(head1);
           int length2 = getCount(head2);
           int diff;
           if (length1 > length2) {
                diff = length1 - length2;
                return _getIntesectionNode(diff, head1, head2);
           } else {
                diff = length2 - length1;
                return _getIntesectionNode(diff, head2, head1);
           }

     }

     int _getIntesectionNode(int d, Node node1, Node node2) {
           int i;
           Node current1 = node1;
           Node current2 = node2;
           for (i = 0; i < d; i++) {
                if (current1 == null) {
                     return -1;
                }
                current1 = current1.getNextNode();
           }
           while (current1 != null && current2 != null) {
                if (current1.getData() == current2.getData()) {
                     return current1.getData();
                }
                current1 = current1.getNextNode();
                current2 = current2.getNextNode();
           }

           return -1;
     }

     int getCount(Node node) {
           Node current = node;
           int count = 0;
           while (current != null) {
                count++;
                current = current.getNextNode();
           }
           return count;
     }

     Node _getIntersectionNodeUsingLoop(Node head1, Node head2) {
          
           Node outerNode,innerNode;
           int length1=getCount(head1),length2=getCount(head2);
           if (length1 > length2) {
                outerNode = head1; innerNode = head2;
           } else {
                outerNode = head2; innerNode = head1;
           }
            
           while (outerNode != null) {
                while (innerNode != null) {
                     if (outerNode == innerNode)
                           return outerNode;
                     innerNode = innerNode.getNextNode();
                }
                innerNode=head2;
                outerNode = outerNode.getNextNode();
           }
           return innerNode;
     }
}

Output:
Intersection Node is 9
Intersection Node is 9


Monday, June 6, 2016

Class Loader in java

Applications written in statically compiled programming languages, such as C and C++, are compiled into native, machine-specific instructions and saved as an executable file. The process of combining the code into an executable native code is called linking - the merging of separately compiled code with shared library code to create an executable application. This is different in dynamically compiled programming languages such as Java. In Java, the .class files generated by the Java compiler remain as-is until loaded into the Java Virtual Machine (JVM) -- in other words, the linking process is performed by the JVM at runtime. Classes are loaded into the JVM on an 'as needed' basis. And when a loaded class depends on another class, then that class is loaded as well.
When a Java application is launched, the first class to run (or the entry point into the application) is the one with the public static void method called main(). This class usually has references to other classes, and all attempts to load the referenced classes are carried out by the class loader.
To get a feeling of this recursive class loading as well as the class loading idea in general, consider the following simple class:
public class ClassLoaderDemo {

     public static void main(String[] args) {
           System.out.println("Class Loaded successfully");
     }
}

C:\Users\admin\Desktop>java -verbose:class ClassLoaderDemo

[Loaded java.net.URLClassLoader$2 from C:\Program Files\Java\jre1.8.0_72\lib\rt.
jar]
[Loaded java.text.Format from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar]
[Loaded java.text.MessageFormat from C:\Program Files\Java\jre1.8.0_72\lib\rt.ja
r]
[Loaded java.util.Locale$Category from C:\Program Files\Java\jre1.8.0_72\lib\rt.
jar]
[Loaded java.util.Locale$1 from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar]
[Loaded java.text.FieldPosition from C:\Program Files\Java\jre1.8.0_72\lib\rt.ja
r]
[Loaded java.util.Date from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar]
[Loaded java.text.AttributedCharacterIterator$Attribute from C:\Program Files\Ja
va\jre1.8.0_72\lib\rt.jar]
[Loaded java.text.Format$Field from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar
]
[Loaded java.text.MessageFormat$Field from C:\Program Files\Java\jre1.8.0_72\lib
\rt.jar]
Error: Could not find or load main class ClassLoaderDemo
[Loaded java.lang.Shutdown from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jre1.8.0_72\lib\rt.ja
r]

When application class is loaded, all other classes required by application class must be loaded by JVM as “on demand basis”
The Java Classloader is a part of the JRE that dynamically loads Java classes into the JVM. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of classloaders.

When the JVM is started, three class loaders are used:
ü  Bootstrap class loader
ü  Extensions class loader
ü  System class loader

Bootstrap class loader
It loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory. This class loader, which is part of the core JVM, is written in native code.

Extensions class loader
The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

System class loader
It loads classes from the current classpath that can be set while invoking a program using -cp or -classpath command line options. This is implemented by the sun.misc.Launcher$AppClassLoader class.


Dynamic Class Loading
Loading a class dynamically is easy. All you need to do is to obtain a ClassLoader and call its loadClass()method. Here is an example:

public class MainClass {

       public static void main(String[] args) {

              ClassLoader classLoader = MainClass.class.getClassLoader();
              System.out.println("Class Loader is " + classLoader);

              try {
                     Class aClass = classLoader.loadClass("com.javadsalgo.MainClass");
                     System.out.println("MainClass.getName() = " + aClass.getName());
              } catch (ClassNotFoundException e) {
                     e.printStackTrace();
              }

       }
}

Output:

Class Loader is sun.misc.Launcher$AppClassLoader@18b4aac2
MainClass.getName() = com.javadsalgo.MainClass

Monday, May 30, 2016

Internal representation of Thread join()


package com.javadsalgo;

class ThreadJoinDemo extends Thread {
     static ThreadJoinDemo thread1;

     public void run() {
           try {
           synchronized (thread1) {
           System.out.println(Thread.currentThread().getName()+ "                  acquired a lock on thread1");
           System.out.println("Going to sleep for 5 minutes");
           System.out.println("If thread1 is locked ?                              "+Thread.holdsLock(thread1));
           Thread.sleep(5000);
           System.out.println(Thread.currentThread().getName()+ "                  completed");
           // System native call invokes thread1.notifyAll(), so return            goes to main thread
                }
           } catch (InterruptedException e) {
       }
     }

     public static void main(String[] ar) throws Exception {
           thread1 = new ThreadJoinDemo();
           thread1.setName("thread1");
           thread1.start();

           synchronized (thread1) {
           // main thread will enter and acquire the lock on thread1.
           System.out.println(Thread.currentThread().getName() + "                  acquired a lock on inner thread1");
           System.out.println("Going to sleep for 2 minutes");
           Thread.sleep(2000);
           System.out.println("If thread1 is locked                                ?"+Thread.holdsLock(thread1));
           /*
           * join() internally calling thread1.wait(),which means                    main thread executing the code will release the lock on                  thread1 and go on waiting state...Entered to run()*/
           thread1.join();
           System.out.println(Thread.currentThread().getName() + "                  completed");
           }
           System.out.println("If thread1 is locked                                ?"+Thread.holdsLock(thread1));
     }
}

Output:

main acquired a lock on inner thread1
Going to sleep for 2 minutes
If thread1 is locked ?true
thread1 acquired a lock on thread1
Going to sleep for 5 minutes
If thread1 is locked ?true
thread1 completed
main completed
If thread1 is locked ?false

Who call notify/notifyAll in case of thread waiting on join method?
After run() method of thread is completed, it doesn't mean thread task is completed, 
It has to do many other tasks like 
  1. Destroying the associated stack, 
  2. Setting the necessary threadStatus etc.
One of the task is notifying the waiting threads, So that Thread waiting on join() method will be notified that thread has completed its task and joined threads can resume.

Above task are executed inside native thread call, so it won't be visible in java thread API.

References: http://javabypatel.blogspot.in/2016/05/how-thread-join-method-works-internally-iava.html