Thursday, May 9, 2013

Get thread stack size at runtime

jinfo -flag ThreadStackSize PID

resource @ https://www.java.net//node/680392

How to get process ID of current JVM

import java.lang.management.ManagementFactory;

public class ProcessID {
   public static void main(String[] args) {
      getProcessId();
   }

   public static String getProcessId() {
      String pname = ManagementFactory.getRuntimeMXBean().getName();
      System.out.println("process name = " + pname);
      String pid = pname;
      int i = pname.indexOf("@");
      if (i!=-1) pid = pname.substring(0,i);
      System.out.println("process id = " + pid);
      return pid;
   }

}


Output:

process name = 7108@KH1205
process id = 7108

NLS.bind(...) vs MessageFormat.format(...)


Follow the discussion on this @ http://dev.eclipse.org/mhonarc/lists/eclipse-dev/msg08041.html

I prefer to use NLS.bind() when I am working with PDE, since this is simple, fast.

Tuesday, May 7, 2013

Java Code Metrics


Google CodePro AnalytiX - Metrics


http://sourceforge.net/projects/metrics/


I liked Google Analytix. It is simple, easy to use, fast and accurate


Update site @ 
http://dl.google.com/eclipse/inst/codepro/latest/3.6   for eclipse 3.6


Tested Results for my plug-in:



Lines of Code: 5,18,178
This is a count of the number of lines in the target elements that contain characters other than white space and comments

Number of Lines:  This represents lines of code with comments.

Global Results

Metric Name
Value
Abstractness
4.8%
Average Block Depth
1.18
Average Cyclomatic Complexity
2.94
Average Lines Of Code Per Method
12.96
Average Number of Constructors Per Type
0.45
Average Number of Fields Per Type
2.84
Average Number of Methods Per Type
5.79
Average Number of Parameters
0.90
Comments Ratio
5%
Efferent Couplings
3,302
Lines of Code
518,178
Number of Characters
26,077,514
Number of Comments
26,156
Number of Constructors
2,636
Number of Fields
27,006
Number of Lines
695,811
Number of Methods
33,343
Number of Packages
220
Number of Semicolons
308,386
Number of Types
5,751
Weighted Methods
107,977

Why would one mark local variables and method parameters as “final” in Java?

String literal can be replaced by a character literal

The single character string literal can be replaced by a character literal to improve performance.

Example:


Given two functions:
public static String concatString(String cs) {
    return "hello" + cs + "world";
}

public static String concatChar(char cc) {
    return "hello" + cc + "world";
}
after examination of the bytecode it boils down to two AbstractStringBuilder.append(String) vs.AbstractStringBuilder.append(char).
Both methods invoke AbstractStringBuilder.expandCapacity(int)) which will allocate a new char[]eventually and System.arraycopy the old content first.
Afterwards AbstractStringBuilder.append(char) just has to put the given char in the array whereas AbstractStringBuilder.append(String) has to check a few constraints and calls String.getChars(int, int, char[], int) which does another System.arraycopy of the appended string.


Trial:



package kondal.performance;

public class StringTest {

public static void main(String[] args) {

long start = System.currentTimeMillis();

StringBuffer message1 = new StringBuffer("Hello world");
for (int i = 0; i < 1000000; i++) {
message1.append("Program");
message1.append("!");
}
long diff = (System.currentTimeMillis() - start);
System.out.println("Totat time(string) in ms:" + diff);


long start2 = System.currentTimeMillis();

StringBuffer message2 = new StringBuffer("Hello world");
for (int i = 0; i < 1000000; i++) {
message2.append("Program");
message2.append('!');
}
long diff2 = (System.currentTimeMillis() - start2);
System.out.println("Totat time(Char) in ms:" + diff2);

}
}


Results:
Totat time(string) in ms:154
Totat time(Char) in ms:116






Friday, May 3, 2013

Java script - Prototype and new


Basically I was looking for how 'new' and 'prototype' things works out in Java script, and I could find very good site, which has lot of stuff on these areas.

http://ejohn.org/apps/learn/#65

Example #1:


RecordManager= function(){};

RecordManager.prototype.getRecords = function(){
  return true;
};

var manager = new RecordManager();

assert(manager.getRecords(), "status");

OutputPASS status


Example #2:


function RecordManager(){}

RecordManager.prototype.addRecords= function(){
  return true;
};

var manager1 = RecordManager();
assert( manager1 , "Is undefined, not an instance of RecordManager." );

var manager2= new RecordManager();
assert( manager2.addRecords(), "Method exists and is callable." );

Output:

  1. FAIL Is undefined, not an instance of RecordManager.
  2. PASS Method exists and is callable.