Tuesday, September 28, 2010

Interview-java-

http://www.javaperformancetuning.com/tips/appservers.shtml#REF9




Class Loader

Typically class loaders are arranged in a parent/child hierarchy. When a class loading request is presented to a class loader, it first asks its parent class loader to fulfill the request. The parent, in turn, asks its parent for the class until the request reaches the top of the hierarchy. If the class loader at the top of the hierarchy cannot fulfill the request, then the child class loader that called it is responsible for loading the class. If the child is also unable to load the class, the request continues back down the hierarchy until a class loader fulfills it or aClassNotFoundException is produced by the class loader at the bottom of the hierarchy.


Figure 1 illustrates a basic class loading hierarchy. Note that a class loaded at a given level in the hierarchy may not reference any classes loaded at a lower level in the hierarchy. Stated another way, a class loader has no visibility into classes loaded by its descendants. In figure 1, if class Foo is loaded by class loader B, and Foodepends on class Baz, then class Baz must be loadable by either class loader A or B. If Baz is only visible to class loader C or D, then a ClassNotFoundException will occur.

ArrayList

Now for some implementation notes. The ArrayList is actually encapsulating an actualy Array, an Object[]. When you instanciate ArrayList, an array is created, and when you add values into it, the array changes its size accordingly. This gives you strengths and weaknesses:

  • Fast Random Access
  • You can perform random access without fearing for performence. Calling get(int) will just access the underlying array.

  • Adding values might be slow When you don’t know the amount of values the array will contain when you create it, a lot of shifting is going to be done in the memory space when the ArrayList manipulates its internal array.
  • Slow manipulation When you’ll want to add a value randomly inside the array, between two already existing values, the array will have to start moving all the values one spot to the right in order to let that happen.

LinkedList

The LinkedList is implemented using nodes linked to each other. Each node contains aprevious node link, next node link, and value, which contains the actual data. When new data is inserted, a node is inserted and the links of the surrounding nodes are updated accordingly. When one is removed, the same happens – The surrounding nodes are changing their links and the deleted node is garbage collected. This, as well, gives strengths and weaknesses:

  • Fast manipulation As you’d expect, adding and removing new data anywhere in the list is instantanious. Change two links, and you have a new value anywhere you want it.
  • No random access Even though the get(int) is still there, it now just iterates the list until it reaches the index you specified. It has some optimizations in order to do that, but that’s basically it.

Some Conclusions

ArrayList is very useful when a well defined set of data is needed in a List interface as opposed to an array. It can be dynamically changed, but try not to do so frequently throughout the life of the application. LinkedList is there for you to do just that:Manipulating it is very easy, and as long as its used for iteration purposes only and not for random accessing, it’s the best solution. Further, if you need random accessing from time to time, I suggest toArray for that specific moment.

Another point I didn’t raise here is the Queue issue. LinkedList implements extended abilities to the normal List interface which allows it to add and remove elements from its beginning and end. This makes the LinkedList perfect for Queue and Stack purposes – Although in Java 5 they already added a Stack class.

Hope this helped someone. Tell me if you want to differ.



SoftReferences are typically used for implementing memory caching. The JVM should try to keep softly referenced objects in memory as long as possible, and when memory is low clear the oldest soft references first. According to the JavaDoc, there are no guarantees though.

WeakReferences is the reference type I use most frequently. It's typically used when you want weak listeners or if you want to connect additional information to an object (using WeakHashMap for example). Very useful stuff when you want to reduce class coupling.

Phantom references can be used to perform pre-garbage collection actions such as freeing resources. Instead, people usually use the finalize() method for this which is not a good idea. Finalizers have a horrible impact on the performance of the garbage collector and can break data integrity of your application if you're not very careful since the "finalizer" is invoked in a random thread, at a random time.

In the constructor of a phantom reference, you specify a ReferenceQueue where the phantom references are enqueued once the referenced objects becomes "phantom reachable". Phantom reachable means unreachable other than through the phantom reference. The initially confusing thing is that although the phantom reference continues to hold the referenced object in a private field (unlike soft or weak references), its getReference() method always returns null. This is so that you cannot make the object strongly reachable again.

From time to time, you can poll the ReferenceQueue and check if there are any new PhantomReferences whose referenced objects have become phantom reachable. In order to be able to to anything useful, one can for example derive a class from java.lang.ref.PhantomReference that references resources that should be freed before garbage collection. The referenced object is only garbage collected once the phantom reference becomes unreachable itself.

  • Performance
    • How quickly must the system respond to interactive operations of different kinds?
    • Are there different classes of interactive operations that users have different tolerances / expectations for?
    • Is there a batch window? What runs in it?
    • Do the batches have their own performance constraints, e.g., to clear the batch window before it closes?
    • Does the batch load influence any interactive users running at the same time?
    • Is there data with a high read/write access ratio that can be cached in memory at different tiers in the architecture?
    • What are the expected performance bottlenecks?
      • CPU?
      • Memory on client, server or intermediate nodes?
      • Hard drive space on each node?
      • Communications links?
      • DB
        • Access
        • Searching
        • Complex joins
      • Interaction with other internal systems?
      • Interaction with systems in other departments?
      • Interaction with partner systems?
      • Interactions with public systems?
  • Scalability
    • Peak load of how many users doing what kinds of operations?
    • Ability to grow to how many records in which critical database tables without slowing down related operations by more than X
    • Avoiding saturating a communication link that cannot be upgraded to a higher speed?
    • What dimensions can be scaled, e.g., more CPUs, more memory, more servers, geographical distribution?
    • Is the primary scaling strategy to "scale up" or to "scale out" -- that is, to upgrade the nodes in a fixed topology, or to add nodes?
  • Availability
    • What is the required uptime percentage?
    • Does this vary by time of day or location?
    • What is the current schedule of controlled outages? Is this acceptable, or is there a goal to improve it?
  • Reliability
    • Are there components with reliabilities that are known to be less than the required reliability of the system?
    • What strategies are currently in place to build more reliable capabilities out of less reliable capabilities?
    • What is the expected mean time to failure by failure severity by operation?
    • How will reliability be assessed prior to deployment?
  • Security
    • What operations need to be secured?
    • How will users be administered?
    • How will users be given permissions to access secured operations?
    • What are the different levels of security and how do these map
      • Security by operation
      • Security by type of object
      • Security by instance of object
  • Maintainability
    • Are there concerns about the ability to hire appropriate technology skills, attract them to the area at reasonable prices?
    • What kinds of changes are anticipated in the first rounds of maintenance? What are their relative priority?
    • What sort of regression testing is required to ensure that maintenance changes do not degrade existing functionality?
    • What sort of maintenance documentation is expected to be produced? When?
  • Flexibility
    • Is there system behavior that needs to be changed regularly without program changes?
      • Can this be encoded in the database?
      • Are there run-time rules that can be handled using a rules interpretation engine?
      • Are there functions that should be user scripted? If so, how will these be QA-ed?
  • Configurability
    • What parameters need to be set on a machine-by-machine basis?
  • Personalizability
    • What aspects of the system can be customized on a per-user basis?
    • How does the user change these settings?
    • What is the strategy for defaults?
  • Usability
    • Are there operations that need to be done as quickly as possible, so that user gestures should be minimized?>
    • Are there difficult or occasional-user operations that require non-standard presentations to help the user perform correctly?
    • What is the balance between data integrity and the ability to stop in a "work in progress" state?
    • What styles of validation are used in what situations?
    • What metaphors from existing or parallel systems should be used?
    • What sort of training deliverables are expected?
    • What sort of on-board help system is expected?
  • Portability
    • Data portability between this system and other systems?
    • Portability across different versions of a single vendor's DB?
    • Ability to port to a different vendor's DB? Which one(s)? When?
    • Browser portability? What browser versions? Historical and future?
    • Operating system portability?
  • Conformance to standards
    • What legal standards apply?
    • What technical standards apply?
    • Other standards, e.g., 508.1 for disabled users?
    • What development standards apply?
      • Database naming standards
      • Existing internal architectural standards (e.g., everything goes in an Oracle database)
      • Language and coding standards
      • Testing and review standards
      • Presentation standards, e.g., use of standard colors, controls or other affordances?
      • Lifecycle models or methodologies
  • Internationalizability
    • What languages?
    • In what order?
    • How translated?
    • Single or multi byte character sets?
  • Efficiency -- space and time
  • Responsiveness
    • What are the expected and upper limit response times per operation in the system?
    • What is the trade-off between lower averages and wider variations in response time?
  • Interoperability
    • What systems will this system interoperate with immediately?
    • What other systems are anticpated?
    • What classes of internal and external systems might later be needed to interoperate with?
    • What functionality from this system needs to be exposed as a service in a service oriented architecture?
    • What functionality from this system needs to be exposed as a Web service or via a portal?
  • Upgradeability
    • Do the servers need to be upgraded while running?
    • How many client stations need to be upgraded, and what are the costs and mechanisms for upgrading them?
    • How often do different kind of fixes need to be distributed? Are there "hot fixes" that have to go out right away, but others that can wait? How often do each kind occur?
  • Auditability / traceability
    • What record of who did what when must be maintained?
    • For how long?
    • Who accesses the audit trails?
    • How?
    • Is archive to tape or other off-site storage media required?
    • Is "effective dating" required?
  • Transactionality
    • What are the important database and application transaction boundaries?
    • Is standard "optimistic" locking appropriate, or is something more complex required in some or all cases>
    • Is disconnected operation required by any node?
  • Administrability
    • What live usage information needs to be displayed?
    • To who? How? When?
    • What "live" interventions are required?
    • What ability to handle remote configurations are required?
    • Are there existing application management consoles that will be used to manage this application?
  • Lots of others -- what are your favorites?

Tuesday, September 7, 2010

Data Structure

Tree traversal
public Node find(int key) // find node with given key
{ // (assumes non-empty tree)
Node current = root; // start at root
Finding a Node 377
while(current.iData != key) // while no match,
{
if(key <>
current = current.leftChild;
else
current = current.rightChild; // or go right?
if(current == null) // if no child,
return null; // didn’t find it
}
return current; // found it
}

-=====================
Inserting in a tree
public void insert(int id, double dd)
{
Node newNode = new Node(); // make new node
newNode.iData = id; // insert data
newNode.dData = dd;
if(root==null) // no node in root
root = newNode;
else // root occupied
{
Node current = root; // start at root
Node parent;
while(true) // (exits internally)
{
parent = current;
if(id <>
{
current = current.leftChild;
if(current == null) // if end of the line,
{ // insert on left
parent.leftChild = newNode;
return;
}
} // end if go left
else // or go right?
{
380 CHAPTER 8 Binary Trees
current = current.rightChild;
if(current == null) // if end of the line
{ // insert on right
parent.rightChild = newNode;
return;
}
} // end else go right
} // end while
} // end else not root
} // end insert()
// -------------------------------------------------------------



Quick sort

function quicksort(array)      var list less, greater      if length(array) ≤ 1          return array      select and remove a pivot value pivot from array      for each x in array          if x <>then append x to less          else if x > pivot then append x to greater      return concatenate(quicksort(less), pivot, quicksort(greater))

Shell sort
inc ← round(n/2)
while inc > 0 do:
for i = inc .. n − 1 do:
temp ← a[i]
j ← i
while j ≥ inc and a[j − inc] > temp do:
a[j] ← a[j − inc]
j ← j − inc
a[j] ← temp
inc ← round(inc / 2.2)


Quick sort
As you can see, there are three basic steps:
1. Partition the array or subarray into left (smaller keys) and right (larger keys)
groups.
Quicksort 333
2. Call ourselves to sort the left group.
3. Call ourselves again to sort the right group.

Thursday, September 2, 2010

products considering

1) Classification.
a) open source product - Statistical knowledge lacking.
b) configurable product.

2) Cloud computing is in demand.