Saturday, 14 May 2016

Linux Bash Shell Terminal Shortcuts

In this article let’s see a few Linux Bash Shell Terminal Shortcuts which can come handy in everyday Linux usage. It is assumed that the default bash settings are not altered.


1) TAB
Auto completion of file or command


2) SHIFT + Page Up/Down
Go up/down the Terminal


3) !!
Repeat last command


4) CTRL D
Logout


5) CTRL L
Clear the Terminal


6) CTRL R
Reverse search history


7) CTRL A
Cursor to the start of Line


8) CTRL E
Cursor to the end of line


9) CTRL U
Delete left of the cursor


10) CTRL K
Delete right of the cursor


11) CTRL W
Delete word on the left


12) CTRL Y
Paste (after CTRL U, K or W)


13) CTRL Z
Suspends the current process and returns shell prompt (fg to resume in foreground or bg to resume in background)


14) CTRL H
Erases a character


15) CTRL C or [Delete]
Interrupt a command


16) CTRL S
Stop scrolling of Screen output and locks keyboard


17) CTRL Q
Resume scrolling of Screen output and unlocks keyboard


18) CTRL \
Kills running command but creates a core file containing the memory image of program


19) CTRL J
Alternative to Enter. Can come handy if enter button of your keyboard is not working properly :)


20) CTRL M
Same as CTRL J


21) Up arrow [↑]
Bash history search. (Newer to older command)


22) Down arrow [↓]
Bash history search. Applicable only after up arrow. (Older to newer command)


23) ALT CTRL T
Open the Terminal (Ubuntu)


24) CTRL SHIFT T
Open a new tab in Terminal (Ubuntu)


Thursday, 28 April 2016

Java Reflection

Introduction -
Reflection is a powerful feature of a programming language that enables it to provide the ability to examine, inspect, “reflect on” or modify the structure of programs at runtime. Not all programming languages support it but the concepts and the principles related to it are usually the same in languages that support it.

Reflection in Java -
In Java, Reflection is a mechanism by which it exposes the features of a class during runtime thereby making it possible to enumerate and access the class’ fields, constructors, methods as objects. More specifically, Java provides object-based mirrors that reflect the Java object model and these objects can be used to access the object’s features at runtime using the runtime API constructs instead of compile-time language constructs.

Each object instance has a getClass() method, inherited from java.lang.Object, which returns an object with the runtime representation of that object's class; this object is an instance of the java.lang.Class, which in turn has methods that return the fields, methods, constructors, superclass, and other properties of that class. We can use these reflection objects to access fields, invoke methods, or instantiate instances, all without having compile-time dependencies on those features.

Reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible. But it should be used carefully as it is rightly said, “With great power comes great responsibility”. If it is possible to perform a task without reflection, then it is preferable to avoid using it because of some concerns related with reflection.

Performance concerns -
The Java virtual machine performs some internal optimizations for normal non-reflective code but as Reflection involves types that are dynamically resolved, these optimizations are not performed as a result of which the reflective operations are slower compared to their non-reflective counterparts. So reflection should be strictly avoided in sections of frequently accessed code in performance sensitive applications.

Exposure of Internals -
The use of reflection can cause unexpected side-effects, since it allows code to perform operations that are strictly speaking not legal in non-reflective code, such as accessing private fields and methods.

Demonstration -
Let’s see an example now where we will load a class “Employee” and access its fields and invoke its methods using reflection.

Employee.java
The java class whose fields will be accessed and methods will be invoked using reflection.

package reflectiondemo;

public class Employee {

   public int EmployeeAge;
   public String EmployeeName;

   public void printEmployeeName() {
       System.out.println("Employee Name : " + EmployeeName);
   }
   
   public void printEmployeeAge() {
       System.out.println("Employee Age : " + EmployeeAge);
   }

   public void setAndPrintEmployeeAge(int Age) {
       EmployeeAge = Age;
       System.out.println("Employee Age : " + EmployeeAge);
   }
}

ReflectionDemo.java
This class will load the “Employee” class, create it’s new instance and access its fields and call its methods at runtime. The code is supplemented by self-explanatory comments.

package reflectiondemo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionDemo {
   public static void main(String[] args) {
       try {
           //a Class array for invoking a no paramater method
           Class noparams[] = {};

           //a Class array for invoking a method with int parameter
           Class[] paramInt = new Class[1];
           paramInt[0] = Integer.TYPE;

           //load the Employee class at runtime
           Class cls = Class.forName("reflectiondemo.Employee");
           
           //create a new instance of the Employee class
           Object obj = cls.newInstance();

           //get the field EmployeeAge
           Field fieldEmployeeAge = cls.getField("EmployeeAge");

           //set the EmployeeAge of the instance of Employee class
           fieldEmployeeAge.set(obj, 25);

           //get the field EmployeeName
           Field fieldEmployeeName = cls.getField("EmployeeName");

           //set the EmployeeName of the instance of Employee class
           fieldEmployeeName.set(obj, "John");

           //call the printEmployeeName method
           Method method = cls.getDeclaredMethod("printEmployeeName",noparams);
           method.invoke(obj, null);

           //call the printEmployeeAge method
           method = cls.getDeclaredMethod("printEmployeeAge", noparams);
           method.invoke(obj, null);

           //call the setAndPrintEmployeeAge method, pass a int param
           method = cls.getDeclaredMethod("setAndPrintEmployeeAge", paramInt);
           method.invoke(obj, 35);

       } catch (Exception ex) {
           ex.printStackTrace();
       }
   }
}


Output -
Employee Name : John
Employee Age : 25
Employee Age : 35


Conclusion -
To conclude, we have seen an overview of Reflection in Java, a related example demonstrating the use of Reflection API and some concerns related to the usage of reflection which point that it should be used carefully to avoid performance overhead and some related side-effects.


Friday, 8 April 2016

Netbeans - Background Scanning Optimization

Introduction :
People using NetBeans for Java development must have encountered the “Background scanning of projects...” message every now and then. This usually happens on opening the IDE, during project build operations or changing any resource used in an open project externally outside NetBeans.


Background scanning is quite essential and important otherwise some productivity features such as code completion, refactoring, go to definition, etc. which require a knowledge of the source code structure could not be used.


Problem Statement :
Unfortunately it is reported by many NetBeans users that the Background scanning of projects is quite slow and it takes too long to complete before the IDE is usable. This is valid for a range of NetBeans versions (NetBeans 6.x, 7.x, 8.x).


Scanning issues are the most commonly heard “deal-breakers” for the NetBeans IDE and the NetBeans forum is full with bug reports regarding this issue of slow Background scanning of projects and the bug fixes and corrective actions mentioned therein have not worked for me and also might not have worked for many other users as suggested in many Stackoverflow questions and comments.


Solution :
Today I am sharing with you a technique that worked for me in optimizing the Background scanning of projects (particularly large Java projects)


My configuration :
Operating system : Mac OS X Yosemite (version 10.10.5)
Processor : 2.2 GHz Intel Core i7
Memory : 16GB
NetBeans version : 8.0.2


Procedure :
1) In NetBeans IDE, go to Window-->Files. This opens the Files Tab.
2) In the Files Tab for each opened project open the nbproject folder and inside it open the project.properties file.
3) Now in this file below the property "excludes" there are file references listed for all your referred Libraries (JARs)
4) There might be some repeated file references with paths that may be old or on someone else's machine(if you are working in a group and transferred projects from someone's machine)
5) Delete those old path references.
Example -


excludes=
file.reference.xyz.jar=../not/correct/path.jar  //delete this line
file.reference.xyz.jar-1=../correct/path.jar    //remove -1
....
includes=**


6)Also locate the property "javac.classpath" and delete the unnecessary classpath entries corresponding to the deleted references as described above.
Example -


javac.classpath=\
${file.reference.xyz.jar}:\       //keep this line
${file.reference.xyz.jar-1}:\     //delete this line
....
javac.compilerargs=


7) So now the file reference mentioned in the file reference section and the javac.classpath property is same and points to a valid Library (JAR) address on your machine or network.
Example -


excludes=
file.reference.xyz.jar=../correct/path.jar//correct reference & path
....
includes=**
....
javac.classpath=\
${file.reference.xyz.jar}:\ //the correct classpath entry for reference
....
javac.compilerargs=
....


Reason :
The reason I faced the above issue was because my projects were transferred from many machines and had repeated references to many other machines and NetBeans was scanning for those unreachable paths.


Conclusion :
The above procedure works because it prevents NetBeans from scanning unnecessary Library paths that may not be present on your machine/network.

Hope this helps you in improving the background scanning performance of NetBeans.


Saturday, 12 March 2016

Thread Analysis in Java

In my previous article, we had seen a method for analyzing the Heap so as to avoid memory leakage. In this article, we will discuss a methodology for Thread Analysis.

Introduction -
A Multithreaded Java Application handling a large number of Concurrent Users needs to be designed and implemented carefully so as to use the maximum resources available and to reduce the response time for each and every User. By maximum resources, I mean maximum CPU power available at disposal.

But if two or more threads utilize the same resources then a conflict between the threads is inevitable.Thread conflict occurs when a thread is waiting for a lock, held by another thread to be lifted.

A Special type of Thread conflict is a “Deadlock” wherein two or more threads are waiting for other threads to complete their tasks in order to complete their own tasks.

To analyze such issues it is important to know the Thread Status of each and every Thread.
Also in case of an application employing waits and synchronization and facing with random issues like unresponsiveness, slow processing, etc. proper Thread Analysis can help to track the root cause.

We will now see a methodology for Thread analysis using jvisualvm.

Thread Analysis Use case-
  • We will first write a sample program (Console Application) in which we will implement a wait on a thread.
  • We will then run the program and use the Tool jvisualvm to visualize the thread.

1) The Sample program
Class - TestClass
fig. 1. A Sample Class implementing a wait and notify.

As seen above we have implemented a wait on the lobjSynchronizer object in TestClass. Notice the wait is inside a synchronized block so that only one thread can access the code in the synchronized block at a time. 

At the same time we have initiated a separate thread to accept an input from the user and if it is “Y” then we notify the lobjSynchronizer object in a synchronized block due to the reason mentioned earlier.

Class - ThreadMonitorDemo

fig. 2. A Sample Class with main starting TestClass on a new Thread

2) Run the program and start the jvisualvm tool as explained in the earlier post.
2.1 Before Wait
On running the program the console will display a message -
Before Wait.
Enter ‘Y’ to Resume.


This is a State where one Thread (Thread-0 as shown in image below) is in the waiting state and another thread is ready to accept the input from the User.This can be observed in the Java VisualVM tool as shown below.

Thread-0 with the Yellow timeline is the Thread which has encountered the wait on the lobjSynchronizer object in TestClass.

Thread-1 with the Green timeline is the Thread which is ready to accept an input from the User.
ThreadWaitingCropped
fig. 3. Java VisualVM tool snapshot showing Thread-0 in Wait state and Thread-1 in Running State.

Different Thread States as shown in the Tool :
The Thread tab shows a list of live and finished threads. Colors are used to distinguish between different thread states.
  • Running: thread is still running
  • Sleeping: thread is sleeping (method yield() was called on the thread object)
  • Wait: thread was blocked by a mutex or a barrier, and is waiting for another thread to release the lock
  • Park: parked threads are suspended until they are given a permit. Unparking a thread is usually done by calling method unpark() on the thread object
  • Monitor: threads are waiting on a condition to become true to resume execution

The Wait on Thread-0 can also be analysed and seen using the ThreadDump as shown below. (Note to get the Thread Dump click the Thread Dump button in the top right corner of the Threads tab)
ThreadDumpFinal
fig. 4. Java VisualVM Thread Dump showing Thread-0 in Wait state at line 26 in Testclass.java.

2.2 After Wait
Now enter “Y” in the console so that the lobjSynchronizer object is notified and the wait on Thread-0 is lifted.
ThreadRunningCropped
fig. 5. Java VisualVM tool snapshot showing Thread-0’s transformation from Wait state to Running State and Thread-1 completing it’s task.
As seen above the Thread-0’s timeline has transformed from Yellow to Green which means it is now in a running state.

The print statement after wait is also printed on the console and now it is executing a while loop after Wait.

Also notice that the Thread-1’s timeline has transformed from Green to White which means it has completed it’s task (since the inline thread has no task after notifying the lobjSynchronizer object)

Conclusion -
Thread Analysis is an important part of Java Application Development and Java VisualVm provides an effective graphical way for the same.

Hope this will help you in Thread analysis of Java programs.