Tuesday, April 26, 2011

Sort Table 1

Here i have given the sample code of how Sort table works


<html>
<head>
<script>

function compareTRs(oTR1, oTR2)
{
var sValue1 = oTR1.cells[0].firstChild.nodeValue;
var sValue2 = oTR2.cells[0].firstChild.nodeValue;

return sValue2.localeCompare(sValue1);
}

function sortTable(sTableID)
{
var oTable = document.getElementById(sTableID);
var oTBody = oTable.tBodies[0];
var colDataRows = oTBody.rows;
var aTRs = new Array;

for (var i=0; i < colDataRows.length; i++)
{
aTRs[i] = colDataRows[i];
}
aTRs.sort(compareTRs);

var oFragment = document.createDocumentFragment();

for(var i=0; i < aTRs.length; i++)
{
oFragment.appendChild(aTRs[i]);
}

oTBody.appendChild(oFragment);

}
</script>




</head>






<table border="1" id="tblSort">
<thead>
<tr>
<th onclick="sortTable('tblSort');" style='cursor:pointer;'> Last Name</th>
</tr>
</thead>

<body>
<tr>
<tdA> Smith</td>
</tr>
<tr>
<tdB> Johnson</td>
</tr>
<tr>
<td> C Henderson</td>
< /tr>
<tr>
<td> DWilliams </td>
</tr>
< tr>
<tdE> E Gilliam </td>
< /tr>
<tr>
<td>F Walker </td>
</tr>
<tr>
<td>Z </td>
</tr>
</tbody>
</table>
</html>

How it works


Sorting is done by getting the values of the column [dom collections] from the table and storing it to an array then sorting the array values.

Now the tr values are sorted and stored in a array ,

Here the sorted values needs to be appended in the table by replacing old tr,

That is done by creating document fragment and adding all the elements (array values) to it

Then appending all child nodes of document fragment back in to the tbody element

ps: source: Wrox Professional JavaScriptTM for Web Developers

Friday, April 22, 2011

Thread Safety

Thread Safety 

Thread Safety is an important Concept in Java, Mainly used in Multi threading programme  in case  such as When two or more threads need access to a shared resource, they need some way t ensure that the resource will be used by only one thread at a time. The process by   which this is achieved is called synchronization. As you will see, Java provides unique,   language-level support for it.

Main Purpose in details

For example In a Cinema ticket booking website , there is only one last ticket is available means , in that case two users trying to book the last ticket, two threads are accessing a class for example bookTicket(which is about to book ticket based on availability ), In that case what would happen is same ticket is would be booked for two users(but in reality only one ticket is available in the physical world). To prevent this type of problem a concept called synchronization (That is used in thread safety) 

If synchronization is used  in that bookTicket() class  means only one thread can access the class at a time (booking of same tickets for two users is not possible) others thread has to wait until that thread (which accessed the bookTicket() class first)  which enters the class exist