Archive for the ‘J2EE’ Category

While looking at some threading related issue the other day, I used the following commands for diagnostics.

Collecting paging activity information

To collect paging data, use the following command:

vmstat {time_between_samples_in_seconds} {number_of_samples} \
> vmstat.txt
vmstat 10 10 > vmstat.txt

If you start vmstat when the problem occurs, a value of 10 for time_between_samples_in_seconds and 10 for number_of_samples usually ensures that enough data is collected during the problem. Collect the vmstat.txt file 100 seconds later.

Collecting system CPU usage information

You can gather CPU usage information using the following command:

top -b > top.txt

You can then collect the top.txt file.

Collecting process CPU usage information
Gather process and thread-level CPU activity information at the point at which the problem occurs, using the following command:

top -H -b -c > top_threads.txt
cat top_threads.txt

top - 06:22:10 up 192 days, 19:00,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 542 total,   1 running, 541 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.0%us,  0.0%sy,  0.0%ni, 99.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  164936028k total, 160272700k used,  4663328k free,        0k buffers
Swap:        0k total,        0k used,        0k free, 64188236k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
24741 xxx      22   0 xxxg  xxxg  11m S  0.0 50.0   0:00.00 java

or if you would like to look into a specific process using PID then issue

top -H -b -c -p <pid> > top_threads_pid.txt

Allow this command to run for a short time. It produces a file called top_threads_pid.txt.

java -XX:+PrintFlagsFinal -version| grep -i Pre
     intx AllocatePrefetchDistance                  = 192             {product}           
     intx AllocatePrefetchInstr                     = 0               {product}           
     intx AllocatePrefetchLines                     = 4               {product}           
     intx AllocatePrefetchStepSize                  = 64              {product}           
     intx AllocatePrefetchStyle                     = 1               {product}           
     bool AlwaysPreTouch                            = false           {product}           
    uintx CMSAbortablePrecleanMinWorkPerIteration   = 100             {product}           
     intx CMSAbortablePrecleanWaitMillis            = 100             {manageable}        
    uintx CMSMaxAbortablePrecleanLoops              = 0               {product}           
     intx CMSMaxAbortablePrecleanTime               = 5000            {product}           
     bool CMSPermGenPrecleaningEnabled              = true            {product}           
    uintx CMSPrecleanDenominator                    = 3               {product}           
    uintx CMSPrecleanIter                           = 3               {product}           
    uintx CMSPrecleanNumerator                      = 2               {product}           
     bool CMSPrecleanRefLists1                      = true            {product}           
     bool CMSPrecleanRefLists2                      = false           {product}           
     bool CMSPrecleanSurvivors1                     = false           {product}           
     bool CMSPrecleanSurvivors2                     = true            {product}           
    uintx CMSPrecleanThreshold                      = 1000            {product}           
     bool CMSPrecleaningEnabled                     = true            {product}           
     bool CompilerThreadHintNoPreempt               = true            {product}           
    uintx G1HeapRegionSize                          = 0               {product}           
     intx InterpreterProfilePercentage              = 33              {product}           
     intx PreBlockSpin                              = 10              {product}           
     intx PreInflateSpin                            = 10              {pd product}

Constructors

When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors are similar to methods, but with some important differences.

  • Constructor name is class name. A constructors must have the same name as the class its in.
  • Default constructor. If you don’t define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).
  • Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.
  • Differences between methods and constructors.
    • There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value.
    • There is no return statement in the body of the constructor.
    • The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

    These differences in syntax between a constructor and method are sometimes hard to see when looking at the source. It would have been better to have had a keyword to clearly mark constructors as some languages do.

  • this(...) – Calls another constructor in same class. Often a constructor with few parameters will call a constructor with more parameters, giving default values for the missing parameters. Use this to call other constructors in the same class.
  • super(...). Use super to call a constructor in a parent class. Calling the constructor for the superclass must be the first statement in the body of a constructor. If you are satisfied with the default constructor in the superclass, there is no need to make a call to it because it will be supplied automatically.

AND

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. For example:

0101
AND 0011
= 0001

XOR

A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. For example:

0101
XOR 0011
= 0110

In Java, all integer types are signed, and the “<<” and “>>” operators perform arithmetic shifts. Java adds the operator “>>>” to perform logical right shifts, but since the logical and arithmetic left-shift operations are identical, there is no “<<<” operator in Java. These general rules are affected in several ways by the default type promotions; for example, since the eight-bit type byte is promoted to int in shift-expressions,[2] the expression “b >>> 2” effectively performs an arithmetic shift of the byte value b instead of a logical shift. Such effects can be mitigated by judicious use of casts or bitmasks; for example, “(b & 0xFF) >>> 2” effectively results in a logical shift.

Nowadays, modern web-based application development can’t exist without J2EE-based applications. These applications are built on a highly secure and powerful development platform. There are various Open Source applications that can help J2EE development a great deal. In this article, I would like to share with everyone an easy way to kick off your J2EE learning for almost no cost to you. Read on »