<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-180857872122085971</id><updated>2011-04-21T12:01:07.980-07:00</updated><category term='C#'/><category term='sleep'/><category term='pool'/><category term='xml'/><category term='hibernate'/><category term='dom'/><category term='mysql'/><category term='java'/><category term='document'/><category term='java convertor'/><category term='crazy4'/><category term='sax'/><category term='paging'/><category term='games'/><category term='xna'/><category term='duplicate form submission'/><category term='thread pool'/><category term='parsing'/><category term='java to html'/><category term='delay'/><category term='defer process'/><category term='thread'/><category term='struts 2'/><category term='html convertor'/><title type='text'>A Witty Gritty Coder!!</title><subtitle type='html'>I code for my living.. I have aspiration, inspiration and sometime desperations.   I live the life as it comes and trying to live the best. I am trying to do my job better and I fail some time. But you fail only when you stop trying and I haven't stopped yet.. 

You should have figured out what this blog is all about.. Haven't u???</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-4980406463695331967</id><published>2009-02-14T21:13:00.001-08:00</published><updated>2009-02-14T22:11:42.817-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='games'/><category scheme='http://www.blogger.com/atom/ns#' term='crazy4'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>My First XNA Creation!!</title><content type='html'>&lt;div&gt;&lt;div&gt;After spending few days on learning C# (basics, C# has myriad of features than I thought!!) and getting the basics of XNA Game framework. This is my first creation.. a simple ball game which&lt;br /&gt;&lt;div&gt;&lt;div&gt;you will find in many mobile phones. &lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;I named it Crazy4!!&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;Though I have done a similar game in college, this helped me to learn C# and XNA basics (I have been following this approach of "learn-language-through-game-creation" has been working pretty well for me! (no wonder I have difficulty learning perl or javascript!!)&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This game has a BG music (taken from Slumdog Millionaire!) and some music based on the game situtation..&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Wanna take a look, download &lt;a href="http://rapidshare.com/files/198255871/Crazy4.zip"&gt;it here&lt;/a&gt;.. you might need .NET Framework 3.0 or something to get this working. Probably you might even need some additional dll file, I don't know. Do let me know if it works!&lt;/p&gt;&lt;p&gt;Preview:&lt;/p&gt;&lt;a href="http://4.bp.blogspot.com/_oB_ppOd871g/SZew-vCU9QI/AAAAAAAAADg/IOmGx0JUa7g/s1600-h/play.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5302901678076654850" style="width: 400px; height: 300px;" alt="" src="http://4.bp.blogspot.com/_oB_ppOd871g/SZew-vCU9QI/AAAAAAAAADg/IOmGx0JUa7g/s400/play.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://1.bp.blogspot.com/_oB_ppOd871g/SZew-c2KJ4I/AAAAAAAAADY/ygG-RTB_fMY/s1600-h/menu.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5302901673193777026" style="width: 400px; height: 300px;" alt="" src="http://1.bp.blogspot.com/_oB_ppOd871g/SZew-c2KJ4I/AAAAAAAAADY/ygG-RTB_fMY/s400/menu.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-4980406463695331967?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/4980406463695331967/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=4980406463695331967' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4980406463695331967'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4980406463695331967'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2009/02/my-first-xna-creation.html' title='My First XNA Creation!!'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_oB_ppOd871g/SZew-vCU9QI/AAAAAAAAADg/IOmGx0JUa7g/s72-c/play.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-3378503899639442150</id><published>2009-02-13T09:21:00.000-08:00</published><updated>2009-02-14T21:13:05.841-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='struts 2'/><category scheme='http://www.blogger.com/atom/ns#' term='duplicate form submission'/><title type='text'>Preventing double submission..</title><content type='html'>Problem with web app is that, it is not as responsive as desktops (except google apps!), so when you submit the form, some user tend to click the submit button twice, doubting whether they clicked it properly the first time. This causes double submission, which is serious problem in e-com apps..&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.java-samples.com/showtutorial.php?tutorialid=582"&gt;This page&lt;/a&gt;, show how this can be avoided using struts 2 framework! worth the read..&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-3378503899639442150?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/3378503899639442150/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=3378503899639442150' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/3378503899639442150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/3378503899639442150'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2009/02/preventing-double-submission.html' title='Preventing double submission..'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-5076586969101518041</id><published>2009-01-12T10:21:00.001-08:00</published><updated>2009-01-12T10:31:32.809-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='sleep'/><category scheme='http://www.blogger.com/atom/ns#' term='defer process'/><category scheme='http://www.blogger.com/atom/ns#' term='delay'/><title type='text'>A very simple handy script...</title><content type='html'>At work, I use Linux for building our software package. Most of the time we will give build just before lunch, so that during the lunch break build will be done. And we, the lazy developers wait till the last minute to commit.. so people will be waiting for us to commit, so that they can start the build before lunch.. I got this idea today, so that we need not wait till everybody completes. We just delay the launch of the build by say 15 minutes so that you can execute the command and go for your lunch.. the lazy developer will commit in the meantime and the build kick starts without your presence..&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Just 2 lines.. see below:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;echo "Waiting for $1 seconds to launch $2"&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;sleep $1&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;$2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I saved the script as &lt;span style="font-weight: bold;"&gt;defer&lt;/span&gt; and put it in the /usr/local/bin directory. I did chmod to make the file executable.. so if i need to defer my build for 15 minute == 900 seconds&lt;br /&gt;&lt;br /&gt;so my command will be:&lt;br /&gt;&lt;span style="font-style: italic;"&gt;defer 900 "make mytool"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;there you go.. after 15 minutes your make will start... hurray!!&lt;br /&gt;&lt;br /&gt;A realllly simple, handy script...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-5076586969101518041?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/5076586969101518041/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=5076586969101518041' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/5076586969101518041'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/5076586969101518041'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2009/01/very-simple-handy-script.html' title='A very simple handy script...'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-545272906272958660</id><published>2009-01-12T09:36:00.000-08:00</published><updated>2009-01-12T09:43:54.305-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='mysql'/><category scheme='http://www.blogger.com/atom/ns#' term='paging'/><title type='text'>Paging database query</title><content type='html'>This is a very common scenario in web application where we have to show data in the DBMS in pages, since we cannot show indefinitely large quantity data in a browser in one go. I never tried it before and was wonder if it is easy to do that. To my surprise it is really easy to page the data in a web application. O/R mapping tools like hibernate makes the job even more easier.&lt;br /&gt;Every RDBMS systems has a SQL syntax for limiting the amount of data chucked out.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;With MySQL&lt;/span&gt;:&lt;br /&gt;(any query you need to write) LIMIT &lt;starting_record&gt; &lt;record_per_page&gt;{OFFSET}, {RECORD_PER_PAGE}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;With Hibernate:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;Query q = session.createQuery(&lt;span style="color:red;"&gt;"..."&lt;/span&gt;);&lt;br /&gt;q.setFirstResult(start);&lt;br /&gt;q.setMaxResults(length);&lt;br /&gt;&lt;/pre&gt;Pretty simple isn't it.. Please follow the below links for more reading..&lt;br /&gt;&lt;a href="http://www.javalobby.org/java/forums/t63849.html"&gt;Javalobby article on paging&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.devx.com/Java/Article/21383/0/page/3"&gt;Devx article on J2EE paging&lt;/a&gt;&lt;/record_per_page&gt;&lt;/starting_record&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-545272906272958660?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/545272906272958660/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=545272906272958660' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/545272906272958660'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/545272906272958660'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2009/01/paging-database-query.html' title='Paging database query'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-4095213451959117617</id><published>2007-07-20T11:30:00.000-07:00</published><updated>2007-07-20T11:52:43.083-07:00</updated><title type='text'>Using Swing Worker</title><content type='html'>In any GUI application the most important thing is responsiveness to user action. To keep our application responsive requires careful design and coding. As we know all the swing related event will be processed by only one thread called AWT event dispatcher thread. Its the job of this thread to do painting of components shown on the screen. If you block AWT event thread for quite sometime it will make your application look unresponsive.&lt;br /&gt;&lt;br /&gt;In this post, I will share my experience with this and how I overcame it. I will also show you a small code snippet, which you can use it other Swing Application.&lt;br /&gt;&lt;br /&gt;To make your application look responsive you should not do heavy weight operation in AWT thread. For example, on click of a button you need to fetch data from a different network, which will take sometime, so never do this in AWT thread. similarly if you add 2 number on click of a button, you can do it in AWT dispatch thread itself.&lt;br /&gt;&lt;br /&gt;In the sample application that we are going to design, we will have a text box which will be filled by data (after doing heavy weight operation) . We will have 2 different buttons. One button will do the heavy weight operation in AWT Thread itself and other will use swing worker thread.&lt;br /&gt;&lt;br /&gt;The code snippet below shows how it can be done.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;import&lt;/b&gt; &lt;span style="color:BLACK;"&gt;java.awt.GridBagConstraints;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;import&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;java.awt.GridBagLayout;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;import&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;java.awt.event.ActionEvent;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;import&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;java.awt.event.ActionListener;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;import&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;javax.swing.JButton;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;import&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;javax.swing.JFrame;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;import&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;javax.swing.JPanel;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;import&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;javax.swing.JTextField;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;import&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;javax.swing.SwingUtilities;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;SwingWorkerExample &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;implements&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;ActionListener&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;private&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;JFrame frame;&lt;br /&gt;&lt;br /&gt;     JTextField field;&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// create necessary user interface&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;init()&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;          frame = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;JFrame(&lt;/span&gt;&lt;span style="color:RED;"&gt;"Test for Swing Worker"&lt;/span&gt;&lt;span style="color:BLACK;"&gt;);&lt;br /&gt;&lt;br /&gt;          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;&lt;br /&gt;          frame.setSize(300,300);&lt;br /&gt;&lt;br /&gt;          JPanel panel = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;JPanel(&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;GridBagLayout());&lt;br /&gt;&lt;br /&gt;         &lt;br /&gt;&lt;br /&gt;          GridBagConstraints gc = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;GridBagConstraints();&lt;br /&gt;&lt;br /&gt;          field = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;JTextField(20);&lt;br /&gt;&lt;br /&gt;          gc.gridx = gc.gridy = 0;&lt;br /&gt;&lt;br /&gt;          panel.add(field,gc);&lt;br /&gt;&lt;br /&gt;          gc.gridy++;&lt;br /&gt;&lt;br /&gt;          JButton button = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;JButton(&lt;/span&gt;&lt;span style="color:RED;"&gt;"Do Work in background"&lt;/span&gt;&lt;span style="color:BLACK;"&gt;); &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// button that does heavy work in background&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:BLACK;"&gt;button.setActionCommand(&lt;/span&gt;&lt;span style="color:RED;"&gt;"Work"&lt;/span&gt;&lt;span style="color:BLACK;"&gt;);&lt;br /&gt;&lt;br /&gt;          button.addActionListener(&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/span&gt;&lt;span style="color:BLACK;"&gt;);&lt;br /&gt;&lt;br /&gt;          panel.add(button,gc);&lt;br /&gt;&lt;br /&gt;          gc.gridy++;&lt;br /&gt;&lt;br /&gt;          button = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;JButton(&lt;/span&gt;&lt;span style="color:RED;"&gt;"Do Work"&lt;/span&gt;&lt;span style="color:BLACK;"&gt;); &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// button that does heavy work in awt thread ;(&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:BLACK;"&gt;button.setActionCommand(&lt;/span&gt;&lt;span style="color:RED;"&gt;"Work2"&lt;/span&gt;&lt;span style="color:BLACK;"&gt;);&lt;br /&gt;&lt;br /&gt;          button.addActionListener(&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/span&gt;&lt;span style="color:BLACK;"&gt;);&lt;br /&gt;&lt;br /&gt;          panel.add(button,gc);&lt;br /&gt;&lt;br /&gt;          frame.setContentPane(panel);&lt;br /&gt;&lt;br /&gt;          frame.pack();&lt;br /&gt;&lt;br /&gt;          frame.setVisible(true);&lt;br /&gt;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// no click of a mouse button, called from AWT thread&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;actionPerformed(ActionEvent e)&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt;&lt;span style="color:BLACK;"&gt;(e.getActionCommand().equals(&lt;/span&gt;&lt;span style="color:RED;"&gt;"Work"&lt;/span&gt;&lt;span style="color:BLACK;"&gt;))&lt;br /&gt;&lt;br /&gt;          {&lt;br /&gt;&lt;br /&gt;               DelaySwingWorker worker = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;DelaySwingWorker(field);&lt;br /&gt;&lt;br /&gt;               worker.startWork();&lt;br /&gt;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;          {&lt;br /&gt;&lt;br /&gt;               &lt;b&gt;try&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;               {&lt;br /&gt;&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color:BLACK;"&gt;Thread.sleep(10000);&lt;br /&gt;&lt;br /&gt;                    field.setText(&lt;/span&gt;&lt;span style="color:RED;"&gt;"Data fetched : "&lt;/span&gt;&lt;span style="color:BLACK;"&gt; +  25);&lt;br /&gt;&lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;catch&lt;/b&gt;&lt;/span&gt;&lt;span style="color:BLACK;"&gt;(Exception ex)&lt;br /&gt;&lt;br /&gt;               {&lt;br /&gt;&lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// out swing worker thread.&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;abstract&lt;/b&gt; &lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;SwingWorker&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;startWork()&lt;br /&gt;&lt;br /&gt;          {&lt;br /&gt;&lt;br /&gt;               Thread t = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;Thread(&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;Runnable() { &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// create a new thread for heavy operation&lt;br /&gt;&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;run()&lt;br /&gt;&lt;br /&gt;                    {&lt;br /&gt;&lt;br /&gt;                         SwingWorker.&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/span&gt;&lt;span style="color:BLACK;"&gt;.getData(); &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// let the swing worker get the data&lt;br /&gt;&lt;br /&gt;                         &lt;/span&gt;&lt;span style="color:BLACK;"&gt;SwingUtilities.invokeLater(&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;Runnable() {&lt;br /&gt;&lt;br /&gt;                              &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;run()&lt;br /&gt;&lt;br /&gt;                              {&lt;br /&gt;&lt;br /&gt;                                   SwingWorker.&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/span&gt;&lt;span style="color:BLACK;"&gt;.setData(); &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// update the data. This will be called from AWT thread&lt;br /&gt;&lt;br /&gt;                                   // so that you can safely do UI related update with the data&lt;br /&gt;&lt;br /&gt;                              }&lt;br /&gt;&lt;br /&gt;                         }&lt;/span&gt;&lt;span style="color:BLACK;"&gt;);&lt;br /&gt;&lt;br /&gt;                    }&lt;br /&gt;&lt;br /&gt;               });&lt;br /&gt;&lt;br /&gt;               t.start();&lt;br /&gt;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;abstract&lt;/b&gt; &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;getData(); &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// abstract method will be implemented by the actual data fetchers&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;abstract&lt;/b&gt; &lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;setData();&lt;br /&gt;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// a sample class which simulates heavy weight operatoin&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;class&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;DelaySwingWorker &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;extends&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;SwingWorker&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;private&lt;/b&gt; &lt;b&gt;int&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;data;&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;private&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;JTextField field;&lt;br /&gt;&lt;br /&gt;         &lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;DelaySwingWorker(JTextField f)&lt;br /&gt;&lt;br /&gt;          {&lt;br /&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/span&gt;&lt;span style="color:BLACK;"&gt;.field = f; &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// field on which we will show the updated data.&lt;br /&gt;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;         &lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;getData()&lt;br /&gt;&lt;br /&gt;          {&lt;br /&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;try&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;               {&lt;br /&gt;&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color:BLACK;"&gt;Thread.sleep(10000); &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// heavy weight operation&lt;br /&gt;&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color:BLACK;"&gt;data = 20; &lt;/span&gt;&lt;span style="color:GREEN;"&gt;// data fetched&lt;br /&gt;&lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;catch&lt;/b&gt;&lt;/span&gt;&lt;span style="color:BLACK;"&gt;(Exception e)&lt;br /&gt;&lt;br /&gt;               {&lt;br /&gt;&lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;         &lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;setData()&lt;br /&gt;&lt;br /&gt;          {&lt;br /&gt;&lt;br /&gt;               field.setText(&lt;/span&gt;&lt;span style="color:RED;"&gt;"Data fetched is : "&lt;/span&gt;&lt;span style="color:BLACK;"&gt; + 20);&lt;br /&gt;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;static&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;main(String[] args)&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;final&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;SwingWorkerExample ex = &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;SwingWorkerExample();&lt;br /&gt;&lt;br /&gt;          SwingUtilities.invokeLater(&lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;new&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;Runnable() {&lt;br /&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:PURPLE;"&gt;&lt;b&gt;public&lt;/b&gt; &lt;b&gt;void&lt;/b&gt; &lt;/span&gt;&lt;span style="color:BLACK;"&gt;run()&lt;br /&gt;&lt;br /&gt;               {&lt;br /&gt;&lt;br /&gt;                    ex.init();&lt;br /&gt;&lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;          });&lt;br /&gt;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now run the application, click the first button, now minimize and maximize the application. It should look normal.&lt;br /&gt;&lt;br /&gt;now click the second button and minimize and maximize the application, you should see window being grayed out. This is because AWT thread is blocked in a heavy weight operation and hence it cannot dispatch paint event to get the components painted.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-4095213451959117617?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/4095213451959117617/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=4095213451959117617' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4095213451959117617'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4095213451959117617'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/07/using-swing-worker.html' title='Using Swing Worker'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-5458126321333540840</id><published>2007-07-13T21:05:00.001-07:00</published><updated>2007-07-13T21:44:05.496-07:00</updated><title type='text'>Developer's life !</title><content type='html'>Its been quite a long since I updated my blog, because I don't find enough time (rather don't have a solid subject to write on! may be!)  After reaching many developer's blog, who share their knowledge of software development, challenges in software development and solutions, I too have decided to write my experience in the process of software development. Yes I am new to this field where there are many experts, but the information that I may provide is nevertheless.&lt;br /&gt;&lt;br /&gt;So here I am to share my experience, to share problems faced, bottle-necks I faced, hours spent on google trying to find the solution. Really don't know how regular I will be in updating my blog, but any how I have decided to give it a try.&lt;br /&gt;&lt;br /&gt;To gain enough expertise in handling in a large software project is a big challenge and I have just now entered to this field. New jargons, new terms - but life is never interesting without new things. Apart from developing software as a job (that's what I'm getting paid for!), I would love to do that as a hobby as well.&lt;br /&gt;&lt;br /&gt;The stuff that I recently developed can be used for converting java code to html (which does some keyword highlighting). You can use this software to convert java code to html so that it can be posted onto a blog or somewhere. You can download it from &lt;a href="http://javaticks.googlepages.com/java2html"&gt;&lt;span style="font-weight: bold;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Currently I am working on software called JInspector. With this software you can inspect jar files, create jar files, assists you in creating MANIFEST files etc.,&lt;br /&gt;&lt;br /&gt;My goals for creating this piece of software are:&lt;br /&gt;1. Learn about JTree, creating custom cell renderer&lt;br /&gt;2. Learn to use Zip Streams&lt;br /&gt;3. last, but not the least, java swings.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-5458126321333540840?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/5458126321333540840/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=5458126321333540840' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/5458126321333540840'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/5458126321333540840'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/07/developers-life.html' title='Developer&apos;s life !'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-251534506083811488</id><published>2007-05-02T22:17:00.000-07:00</published><updated>2007-05-02T22:25:29.849-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java convertor'/><category scheme='http://www.blogger.com/atom/ns#' term='html convertor'/><category scheme='http://www.blogger.com/atom/ns#' term='java to html'/><title type='text'>Java &lt;-&gt; HTML</title><content type='html'>Have Java code, put it some where on a web page, that looks good and nice as it looks in an IDE. So do it all you need is Java to HTML converter. There are tons available on the net, but that didn't stop me from writing my own.&lt;br /&gt;&lt;br /&gt;A console based JavaHTMLConvertor is ready. Now just have to decorate it with some GUI to make is user friendly. It's on the incubator and I should be able to bake it tomorrow ;) and it will be available for download.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-251534506083811488?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/251534506083811488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=251534506083811488' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/251534506083811488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/251534506083811488'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/05/java-html.html' title='Java &lt;-&gt; HTML'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-1641821791648447340</id><published>2007-05-02T01:45:00.000-07:00</published><updated>2007-05-02T22:16:04.205-07:00</updated><title type='text'></title><content type='html'>Code snippet is available at :&lt;br /&gt;&lt;a href="http://javaticks.googlepages.com/threadPool.html"&gt;&lt;br /&gt;http://javaticks.googlepages.com/threadPool.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;There is a mistake in the above program, which tries to interrupt all the other thread in pool on emergency shutdown. This is not possible. A thread can only interrupt itself and not other threads.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-1641821791648447340?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/1641821791648447340/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=1641821791648447340' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/1641821791648447340'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/1641821791648447340'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/05/package-com.html' title=''/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-6559444366795403280</id><published>2007-05-02T01:31:00.000-07:00</published><updated>2007-05-02T01:43:03.659-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='pool'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='thread'/><category scheme='http://www.blogger.com/atom/ns#' term='thread pool'/><title type='text'>A sample Thread Pool implementation..</title><content type='html'>Many jobs at a time, but no so many!&lt;br /&gt;&lt;br /&gt;We have many thread pool implementations available in Java, but still I would like to create my own, just to understand the concepts and here I am to share that experience with you all..&lt;br /&gt;&lt;br /&gt;Before we start coding, let us first think about the requirement of a thread pool.&lt;br /&gt;&lt;br /&gt;1. We have a pool of thread (may be on demand or not). The no. of threads in a pool is fixed. say 5 or 10.&lt;br /&gt;2. We add jobs to the pool.&lt;br /&gt;3. If any thread is free in the pool, then it would jump in and take up the job.&lt;br /&gt;4. If there are no free threads in pool (that means all the threads are working), the job will be waiting to be handled by some thread, which becomes available after completing the current job.&lt;br /&gt;&lt;br /&gt;That's it. Problem at hand is simple, we just need to code that's it...!!&lt;br /&gt;&lt;br /&gt;ZZZzzzz.. (3 hours past)&lt;br /&gt;&lt;br /&gt;argh..! code is ready for the post. But I have a big problem. I don't want to just copy and paste my java code, it looks dirty.. So I want to convert my Java to HTML so that I can put it here. So I searched the net and found many. Then I thought, why don't I write my own? It's going to be very nice to do it. So I decided to save the post as a draft, and continue it once Java to HTML tool kit is ready..&lt;br /&gt;&lt;br /&gt;Note: For those who think, how do I then posted formatted java code in Previous post, what I usually used to do is, go to &lt;a href="http://forums.sun.com"&gt;http://forums.sun.com&lt;/a&gt; and write the code using code format tag, then click preview and then copy and paste it over here! that's it.. But this time that doesn't work (because of a bug in the HTML formatting in sun website).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-6559444366795403280?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/6559444366795403280/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=6559444366795403280' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/6559444366795403280'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/6559444366795403280'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/05/sample-thread-pool-implementation.html' title='A sample Thread Pool implementation..'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-6084325384029359866</id><published>2007-02-19T03:29:00.002-08:00</published><updated>2007-02-19T03:37:33.612-08:00</updated><title type='text'>Tri-color Marking Garbage Collector</title><content type='html'>This is one type of garbage collector. This collector working is very simple.&lt;br /&gt;All the references were separated into 3 groups. White, Gray and Black.&lt;br /&gt;&lt;br /&gt;Objects that are directly reachable are in White group. (Global , static variables etc). And all the remaining references are put in white. Mostly black list starts empty.&lt;br /&gt;&lt;br /&gt;This algorithm works much like a graph traversal.&lt;br /&gt;1. Start with a node in White list.&lt;br /&gt;2. Find all the references that are reachable from this one.&lt;br /&gt;3. Gray those references. If the reference that are reachable from current node, is already in Gray or Black, then move current node to black list. Proceed this until gray node becomes empty.&lt;br /&gt;4. Now, the reference that are still in White list are not reachable, so they all are eligible for garbage collection.&lt;br /&gt;&lt;br /&gt;For more information about this see:&lt;br /&gt;http://www.memorymanagement.org/glossary/t.html#tri-color.marking&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-6084325384029359866?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/6084325384029359866/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=6084325384029359866' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/6084325384029359866'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/6084325384029359866'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/02/tri-color-marking-garbage-collector_19.html' title='Tri-color Marking Garbage Collector'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-7820224436297890639</id><published>2007-02-15T05:41:00.000-08:00</published><updated>2007-02-15T05:56:37.509-08:00</updated><title type='text'>Garbage Collectors !</title><content type='html'>&lt;b&gt;Garbage collection&lt;/b&gt; (also known as &lt;b&gt;GC&lt;/b&gt;) is a form of automatic &lt;a href="http://en.wikipedia.org/wiki/Memory_management" title="Memory management"&gt;memory management&lt;/a&gt;. The &lt;b&gt;garbage collector&lt;/b&gt; or &lt;b&gt;collector&lt;/b&gt; attempts to reclaim &lt;i&gt;&lt;a href="http://en.wikipedia.org/wiki/Garbage_%28computer_science%29" title="Garbage (computer science)"&gt;garbage&lt;/a&gt;&lt;/i&gt;, or memory used by &lt;a href="http://en.wikipedia.org/wiki/Object_%28computer_science%29" title="Object (computer science)"&gt;objects&lt;/a&gt; that will never again be accessed or mutated by the &lt;a href="http://en.wikipedia.org/wiki/Application_software" title="Application software"&gt;application&lt;/a&gt;. Garbage collection was invented by &lt;a href="http://en.wikipedia.org/wiki/John_McCarthy_%28computer_scientist%29" title="John McCarthy (computer scientist)"&gt;John McCarthy&lt;/a&gt; around &lt;a href="http://en.wikipedia.org/wiki/1959" title="1959"&gt;1959&lt;/a&gt; to solve the problems of manual memory management in his &lt;a href="http://en.wikipedia.org/wiki/Lisp_programming_language" title="Lisp programming language"&gt;Lisp programming language&lt;/a&gt;. &lt;p&gt;Garbage collection is often portrayed as the opposite of &lt;a href="http://en.wikipedia.org/wiki/Manual_memory_management" title="Manual memory management"&gt;manual memory management&lt;/a&gt;, which requires the programmer to specify which objects to deallocate and return to the memory system. However, many systems use a combination of the two approaches, and there are other techniques being studied (such as &lt;a href="http://en.wikipedia.org/wiki/Region_inference" title="Region inference"&gt;region inference&lt;/a&gt;) to solve the same fundamental problem. Note that there is an ambiguity of terms, as theory often uses the terms &lt;b&gt;manual garbage-collection&lt;/b&gt; and &lt;b&gt;automatic garbage-collection&lt;/b&gt; rather than &lt;b&gt;manual memory management&lt;/b&gt; and &lt;b&gt;garbage-collection&lt;/b&gt;, and does not restrict &lt;b&gt;garbage-collection&lt;/b&gt; to &lt;a href="http://en.wikipedia.org/wiki/Memory_management" title="Memory management"&gt;memory management&lt;/a&gt;, rather considering that any logical or physical &lt;a href="http://en.wikipedia.org/wiki/Resource" title="Resource"&gt;resource&lt;/a&gt; may be garbage-collected.&lt;/p&gt;One very good thing about Java is that it relieves programmers from thinking about Memory Management. It takes care of Memory reclaiming automatically. But this feature doesn't come to us free of cost. It costs and the consequences of this slow execution (not much). In olden days of GC, GC runs for upto 25 - 50% of the time. But GC in Java have come a long way.&lt;br /&gt;&lt;br /&gt;In this blog, we will discuss about various types of garbage collectors. How do they work and when to use what type of GC.&lt;br /&gt;&lt;br /&gt;First we will look what a ideal GC should be...&lt;br /&gt;&lt;br /&gt;1. Zero Pause time. That is the time consumed by the GC should be Zero&lt;br /&gt;2. Should be able to take care of memory island.&lt;br /&gt;3. Algorithm should be incremental. That is the GC can stop in the middle and continue later on without any problem.&lt;br /&gt;4. Virtual memory interaction - That is GC should not cause Page fault. This happens when we try to reach the memory area, that is not used by the code for a long time and may be it's eligible for garbage collection. But if GC collector tries to do something(mark &amp; sweep algorithm may have to touch the memory for GC)  with that memory location, then page fault occurs, which causes the memory block to be refetched from Virtual memory.&lt;br /&gt;and many others ...&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;b&gt;Pause time.&lt;/b&gt; Does the collector stop the world to perform collection?  For how long?  Can pauses be bounded in time?&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;Pause predictability.&lt;/b&gt; Can garbage collection pauses be scheduled at times that are convenient for the user program, rather than for the garbage collector?&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;CPU usage.&lt;/b&gt; What percentage of the total available CPU time is spent in garbage collection?&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;Memory footprint.&lt;/b&gt; Many garbage collection algorithms require dividing the heap into separate memory spaces, some of which may be inaccessible to the user program at certain times. This means that the actual size of the heap may be several times bigger than the maximum heap residency of the user program.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;Virtual memory interaction.&lt;/b&gt; On systems with limited physical memory, a full garbage collection may fault nonresident pages into memory to examine them during the collection process. Because the cost of a page fault is high, it is desirable that a garbage collector properly manage locality of reference.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;Cache interaction.&lt;/b&gt; Even on systems where the entire heap can fit into main memory, which is true of virtually all Java applications, garbage collection will often have the effect of flushing data used by the user program out of the cache, imposing a performance cost on the user program.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;Effects on program locality.&lt;/b&gt; While some believe that the job of the garbage collector is simply to reclaim unreachable memory, others believe that the garbage collector should also attempt to improve the reference locality of the user program. Compacting and copying collectors relocate objects during collection, which has the potential to improve locality.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;Compiler and runtime impact.&lt;/b&gt; Some garbage collection algorithms require significant cooperation from the compiler or runtime environment, such as updating reference counts whenever a pointer assignment is performed. This creates both work for the compiler, which must generate these bookkeeping instructions, and overhead for the runtime environment, which must execute these additional instructions. What is the performance impact of these requirements? Does it interfere with compile-time optimizations?&lt;/li&gt;&lt;/ul&gt;A brief overview of each of the algorithm is given the below location:&lt;br /&gt;http://www-128.ibm.com/developerworks/java/library/j-jtp10283/&lt;br /&gt;&lt;br /&gt;In the next post we will take a look at a particular Garbage collector in more details, before that we need to learn a few more terms, which is used that particular algorithm.&lt;br /&gt;&lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;&lt;a id="root" name="root"&gt;root&lt;/a&gt;&lt;/strong&gt;&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;In &lt;a href="http://www.memorymanagement.org/glossary/t.html#tracing.garbage.collection"&gt;tracing garbage collection&lt;/a&gt;, a root holds a &lt;a href="http://www.memorymanagement.org/glossary/r.html#reference"&gt;reference&lt;/a&gt; or set of references to &lt;a href="http://www.memorymanagement.org/glossary/o.html#object"&gt;objects&lt;/a&gt; that are &lt;em&gt;a priori&lt;/em&gt; &lt;a href="http://www.memorymanagement.org/glossary/r.html#reachable"&gt;reachable&lt;/a&gt;.  The &lt;a href="http://www.memorymanagement.org/glossary/r.html#root.set"&gt;root set&lt;/a&gt; is used as the starting point in determining all reachable data.&lt;/p&gt; &lt;p&gt;Roots basically comprise the references in the state of the &lt;a href="http://www.memorymanagement.org/glossary/m.html#mutator"&gt;mutator&lt;/a&gt;.  Typical roots are global variables, other &lt;a href="http://www.memorymanagement.org/glossary/s.html#static.allocation"&gt;static&lt;/a&gt; data, and the &lt;a href="http://www.memorymanagement.org/glossary/c.html#control.stack"&gt;control stack&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;See also:&lt;/strong&gt; &lt;a href="http://www.memorymanagement.org/glossary/w.html#weak.root"&gt;weak root&lt;/a&gt;;     &lt;a href="http://www.memorymanagement.org/glossary/s.html#strong.root"&gt;strong root&lt;/a&gt;;     &lt;a href="http://www.memorymanagement.org/glossary/a.html#ambiguous.root"&gt;ambiguous root&lt;/a&gt;;     &lt;a href="http://www.memorymanagement.org/glossary/e.html#exact.root"&gt;exact root&lt;/a&gt;. &lt;/p&gt;&lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;&lt;a id="root.set" name="root.set"&gt;root set&lt;/a&gt;&lt;/strong&gt;&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;The &lt;i&gt;root set&lt;/i&gt; is the collection of &lt;a href="http://www.memorymanagement.org/glossary/r.html#root"&gt;roots&lt;/a&gt; that the &lt;a href="http://www.memorymanagement.org/glossary/m.html#mutator"&gt;mutator&lt;/a&gt; declares to the &lt;a href="http://www.memorymanagement.org/glossary/c.html#collector-2"&gt;collector&lt;sup&gt;&lt;small&gt;(2)&lt;/small&gt;&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;&lt;/dd&gt;&lt;dt&gt;For more understanding of such terms visit the following link:&lt;/dt&gt;&lt;dd&gt;http://www.memorymanagement.org/glossary/r.html&lt;/dd&gt;&lt;/dl&gt;One algorithm we are going to look at it Tricolor Marking Garbage collector.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-7820224436297890639?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/7820224436297890639/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=7820224436297890639' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/7820224436297890639'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/7820224436297890639'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/02/garbage-collectors.html' title='Garbage Collectors !'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-4805118504905686705</id><published>2007-02-13T07:22:00.000-08:00</published><updated>2007-02-11T23:24:45.490-08:00</updated><title type='text'>When JIT happens ?</title><content type='html'>Since Java works much like a interpreted language, it is argued that Java program will run a lot slower then their native language counter parts. To avoid this Sun came up with Just in Time compiler, which will change the Java byte code to native machine dependent assembly. This will make Java run as fast as an Application created using 'C'.&lt;br /&gt;&lt;br /&gt;This JIT will not happen always. There is criteria at which this will happen. The following things discusses about when JIT will happen and how it can be configured during JVM invocation.&lt;br /&gt;&lt;br /&gt;The following link provides much more details about all the command line options available.&lt;br /&gt;http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp&lt;br /&gt;&lt;br /&gt;Compiler Option: -XX:CompileThreshold=10000&lt;br /&gt;Purpose : Number of method invocations/branches before compiling [-client: 1,500]&lt;br /&gt;&lt;br /&gt;This options specifies that when a particular method is invoked 10000 times, then compile this method to native code.&lt;br /&gt;&lt;br /&gt;INorder to know when the native compilation has happened we need to use the following JVM command line options.&lt;br /&gt;java -XX:+PrintCompilation &lt;class_name&gt;&lt;br /&gt;&lt;br /&gt;The following link provides much more interesting test results. I enjoyed reading it.&lt;br /&gt;http://www.javaworld.com/javaworld/javaqa/2003-04/01-qa-0411-hotspot.html&lt;br /&gt;&lt;br /&gt;So If we could run Java Application at native code speed then why not just do it always. As specified in the above location, the problem could be.....&lt;br /&gt;&lt;br /&gt;As an experiment, you might consider shortening the warm-up period by manipulating the &lt;code&gt;-XX:CompileThreshold&lt;/code&gt; JVM option, although you will soon discover that making this threshold too small will delay your application's startup, as HotSpot begins compiling just about every method it discovers into native code, including methods in the core Java libraries.&lt;br /&gt;&lt;br /&gt;So, it is advised to set the CompileThreshold option to 10000 for Server applications and 1500 for Client application.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-4805118504905686705?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/4805118504905686705/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=4805118504905686705' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4805118504905686705'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4805118504905686705'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/02/when-jit-happens.html' title='When JIT happens ?'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-4607711579692921893</id><published>2007-02-11T22:57:00.000-08:00</published><updated>2007-02-11T22:59:45.024-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='dom'/><category scheme='http://www.blogger.com/atom/ns#' term='xml'/><category scheme='http://www.blogger.com/atom/ns#' term='document'/><title type='text'>Reading simple XML File using DOM Parser</title><content type='html'>In this example, we will write Java code to read the contents of a simplest XML file and print it.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;package&lt;/b&gt;&lt;/span&gt; com;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; java.io.IOException;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; javax.xml.parsers.DocumentBuilder;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; javax.xml.parsers.DocumentBuilderFactory;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; org.w3c.dom.Document;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; org.w3c.dom.Element;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; org.w3c.dom.Node;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; org.w3c.dom.NodeList;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; org.w3c.dom.Text;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;import&lt;/b&gt;&lt;/span&gt; org.xml.sax.SAXException;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;public&lt;/b&gt;&lt;/span&gt; &lt;span style="color:navy;"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt; XMLLesson &lt;span style="color:navy;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:darkgreen;"&gt;/*public String getNodeTextValue(NodeList nl)&lt;br /&gt;{&lt;br /&gt;Node node = null;&lt;br /&gt;for(int i = 0 ; i&lt; node =" nl.item(i);" t =" (Text)"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;public&lt;/b&gt;&lt;/span&gt; &lt;span style="color:navy;"&gt;&lt;b&gt;void&lt;/b&gt;&lt;/span&gt; parseXML(String fname)&lt;br /&gt;&lt;span style="color:navy;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();&lt;br /&gt;DocumentBuilder bld = &lt;span style="color:navy;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;;&lt;br /&gt;Document doc = &lt;span style="color:navy;"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/span&gt;;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;try&lt;/b&gt;&lt;/span&gt; &lt;span style="color:navy;"&gt;{&lt;/span&gt;&lt;br /&gt; bld = fact.newDocumentBuilder();&lt;br /&gt; doc = bld.parse(fname);&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;}&lt;/span&gt; &lt;span style="color:navy;"&gt;&lt;b&gt;catch&lt;/b&gt;&lt;/span&gt;(Exception e)&lt;br /&gt;&lt;span style="color:navy;"&gt;{&lt;/span&gt;&lt;br /&gt; e.printStackTrace() ;&lt;br /&gt;&lt;span style="color:navy;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Element parentElement = doc.getDocumentElement();&lt;br /&gt;NodeList nl = parentElement.getElementsByTagName(&lt;span style="color:red;"&gt;"child1"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;int&lt;/b&gt;&lt;/span&gt; i;&lt;br /&gt;System.out.println(&lt;span style="color:red;"&gt;"Parent Element : "&lt;/span&gt; + parentElement.getTagName()  );&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;for&lt;/b&gt;&lt;/span&gt;(i = 0;i&lt;nl.getlength() i=""&gt;&lt;span style="color:navy;"&gt;&amp;ltnl.getLength();++i) {&lt;/span&gt;&lt;br /&gt; System.out.println(&lt;span style="color:red;"&gt;"i = "&lt;/span&gt; + i + &lt;span style="color:red;"&gt;" "&lt;/span&gt; + nl.item(i).getFirstChild().getNodeValue()  );&lt;br /&gt;&lt;span style="color:navy;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:darkgreen;"&gt;//System.out.println(getNodeTextValue(n));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;b&gt;public&lt;/b&gt;&lt;/span&gt; &lt;span style="color:navy;"&gt;&lt;b&gt;static&lt;/b&gt;&lt;/span&gt; &lt;span style="color:navy;"&gt;&lt;b&gt;void&lt;/b&gt;&lt;/span&gt; main(String[] args) &lt;span style="color:navy;"&gt;{&lt;/span&gt;&lt;br /&gt;XMLLesson l = &lt;span style="color:navy;"&gt;&lt;b&gt;new&lt;/b&gt;&lt;/span&gt; XMLLesson();&lt;br /&gt;l.parseXML(&lt;span style="color:red;"&gt;"c:\\temp.xml"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color:navy;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;}&lt;br /&gt;&lt;br /&gt;Contents of xml file:&lt;br /&gt;&lt;parent&gt;&lt;br /&gt;&lt;child1&gt; child1data &lt;/child1&gt;&lt;br /&gt;&lt;child1&gt; child1data &lt;/child1&gt;&lt;br /&gt;&lt;child1&gt; child1data &lt;/child1&gt;&lt;br /&gt;&lt;/parent&gt;&lt;br /&gt;&lt;br /&gt;Output is :&lt;br /&gt;Parent Element : parent&lt;br /&gt;i = 0  child1data&lt;br /&gt;i = 1  child1data&lt;br /&gt;i = 2  child1data&lt;br /&gt;&lt;br /&gt;Explanation:&lt;br /&gt;This code is written for Java 1.5. This use XML Parser provided by Sun. There are many XML&lt;br /&gt;parsers available, including xerces and xml parser api from IBM.&lt;br /&gt;&lt;br /&gt;All the xml file will have a document element. All the tags will be inside that element&lt;br /&gt;only. So first we get the document element. Then get the list of child nodes associate&lt;br /&gt;with it.&lt;br /&gt;&lt;br /&gt;In order to get the text content of the tag,&lt;br /&gt;&lt;child1&gt;data&lt;/child1&gt;&lt;br /&gt;"node.getFirstChild().getNodeValue()"&lt;br /&gt;node.getFirstChild() return the value node, and calling getNodeValue returns the value.&lt;br /&gt;&lt;br /&gt;you could also use node.getTextContent(), to get the text content of the node.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/nl.getlength()&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-4607711579692921893?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/4607711579692921893/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=4607711579692921893' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4607711579692921893'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4607711579692921893'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/02/reading-simple-xml-file-using-dom.html' title='Reading simple XML File using DOM Parser'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-180857872122085971.post-4589501320698840234</id><published>2007-02-11T20:29:00.000-08:00</published><updated>2007-02-11T21:20:04.203-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='dom'/><category scheme='http://www.blogger.com/atom/ns#' term='sax'/><category scheme='http://www.blogger.com/atom/ns#' term='parsing'/><category scheme='http://www.blogger.com/atom/ns#' term='xml'/><title type='text'>XML Parsers</title><content type='html'>&lt;span style="font-weight: bold;"&gt;XML Parsing in Java:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:Verdana,Arial,Helvetica,sans-serif;font-size:85%;"  &gt;&lt;span style="font-weight: bold;"&gt;XML parser          is a software module to read documents and a means to provide access to          their content. XML parser generates a structured tree to return the results          to the browser. An XML parser is similar to a processor that determines          the structure and properties of the data. An XML parser can read a XML          document to create an output to generate a display form.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-size:100%;"&gt;Types of XML Parser:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1. SAX Parser&lt;br /&gt;2. DOM Parser&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;SAX Parser:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:Verdana,Arial,Helvetica,sans-serif;font-size:85%;"  &gt;&lt;span style="font-weight: bold;font-size:85%;" &gt;Simple API         for XML (SAX) was developed by the members of a public mailing list (XML-DEV).It         gives an event based approach to XML parsing. It means that instead of         going from node to node, it goes from event to event. SAX is an event         driven interface. Events include XML tag, detecting errors etc,&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:Verdana,Arial,Helvetica,sans-serif;font-size:85%;"  &gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-size:100%;"&gt;DOM Parser:&lt;/span&gt;&lt;br /&gt;DOM Parser stands for Document Object Model. This read the entire XML Files and puts in the memory. This makes traversal easy and efficient. This should only be used for small XML Files. This operates faster, but takes up more memory.&lt;br /&gt;&lt;br /&gt;In the next post, we will see an example of DOM Parser in Java.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/180857872122085971-4589501320698840234?l=witty-gritty-coder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://witty-gritty-coder.blogspot.com/feeds/4589501320698840234/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=180857872122085971&amp;postID=4589501320698840234' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4589501320698840234'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/180857872122085971/posts/default/4589501320698840234'/><link rel='alternate' type='text/html' href='http://witty-gritty-coder.blogspot.com/2007/02/xml-parsers.html' title='XML Parsers'/><author><name>Listen2Gopi</name><uri>http://www.blogger.com/profile/18401124299900996551</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://3.bp.blogspot.com/_oB_ppOd871g/SZezqop3aqI/AAAAAAAAADo/kzlTaDuMyHk/S220/e2.JPG'/></author><thr:total>1</thr:total></entry></feed>
